简体   繁体   中英

Hibernate Criteria multiple where

Good morning, I am trying to create a method to return several games from my MySQL DB using Hibernate Criteria. The method receives a Product object which can have multiple parameters at null. I'm working on Hibernate 5.2 right now.

My product class is this:

@Entity
@Table(name = "Producto", catalog = "gzone")
public class Producto {

private Long idProducto = null;
private String nombre = null;
private Double precio = null;
private Integer anio = null;
private String requisitos = null;
private Oferta oferta = null;

private Set<Producto_Idioma> producto_idioma = new HashSet<Producto_Idioma>(0);

private List<Categoria> categorias = null;
private List<NJugadores> njugadores = null;
private List<Idioma> idioma = null;

public Producto () {
    categorias = new ArrayList<Categoria>();
    idioma = new ArrayList<Idioma>();
    njugadores = new ArrayList<NJugadores>();
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_producto", unique = true, nullable = false)
public Long getIdProducto() {
    return idProducto;
}

public void setIdProducto(Long idProducto) {
    this.idProducto = idProducto;
}

@Column(name = "nombre", unique = false, nullable = false)
public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

@Column(name = "precio", unique = false, nullable = false)
public Double getPrecio() {
    return precio;
}

public void setPrecio(Double precio) {
    this.precio = precio;
}

@Column(name = "anio", unique = false, nullable = false)
public Integer getAnio() {
    return anio;
}

public void setAnio(Integer i) {
    this.anio = i;
}

@Column(name = "requisitos", unique = false, nullable = true)
public String getRequisitos() {
    return requisitos;
}

public void setRequisitos(String requisitos) {
    this.requisitos = requisitos;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_oferta", nullable = false)
public Oferta getOferta() {
    return oferta;
}

public void setOferta(Oferta oferta) {
    this.oferta = oferta;
}

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Producto_Categoria", catalog = "gzone", joinColumns = {
@JoinColumn(name = "id_producto", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "id_categoria",
                nullable = false, updatable = false) })
public List<Categoria> getCategorias() {
    return categorias;
}

public void setCategorias(List<Categoria> categorias) {
    this.categorias = categorias;
}

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Producto_NJugadores", catalog = "gzone", joinColumns = {
@JoinColumn(name = "id_producto", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "id_njugador",
                nullable = false, updatable = false) })
public List<NJugadores> getNjugadores() {
    return njugadores;
}

public void setNjugadores(List<NJugadores> njugadores) {
    this.njugadores = njugadores;
}

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Producto_Idioma_Disponible", catalog = "gzone", joinColumns = {
@JoinColumn(name = "id_producto", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "id_idioma",
                nullable = false, updatable = false) })
public List<Idioma> getIdioma() {
    return idioma;
}

public void setIdioma(List<Idioma> idioma) {
    this.idioma = idioma;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.producto")
public Set<Producto_Idioma> getProducto_Idioma() {
    return this.producto_idioma;
}

public void setProducto_Idioma(Set<Producto_Idioma> producto_idioma) {
    this.producto_idioma = producto_idioma;
}

}

And this is the Hibernate Criteria code:

@Override
    public List<Producto> findByCriteria(Session session, ProductoCriteria productoCriteria, int startIndex, int count, String idioma)
            throws DataException {

        try {

             CriteriaBuilder builder = session.getCriteriaBuilder();

             Join<Producto, Oferta> ofertaJoin = null;
             Join<Producto, Categoria> categoriaJoin = null;
             Join<Producto, Idioma> idiomaJoin = null;
             Join<Producto, NJugadores> jugadoresJoin = null;

             CriteriaQuery<Producto> criteria = builder.createQuery( Producto.class );
             // Seleccionamos la tabla orgien (En este caso producto)
             Root<Producto> producto = criteria.from( Producto.class );

             //Seleccionamos la tabla producto
             criteria.select(producto);

             // Hacemos un JOIN de la tabla oferta que es una @ManyToOne
             if (productoCriteria.getOferta()!=null) 
                 ofertaJoin = producto.join(ParameterNamesCriteria.OFERTA);

             // Hacemos un JOIN de la tabla oferta que es una @ManyToMany
             if (!productoCriteria.getCategorias().isEmpty()) 
                 categoriaJoin = producto.join(ParameterNamesCriteria.CATEGORIA);         

             // Hacemos un JOIN de la tabla idioma que es una @ManyToMany
             if (!productoCriteria.getIdioma().isEmpty()) 
                 idiomaJoin = producto.join(ParameterNamesCriteria.IDIOMA);          

             // Hacemos un JOIN de la tabla idioma que es una @ManyToMany
             if (!productoCriteria.getNjugadores().isEmpty()) 
                 jugadoresJoin = producto.join(ParameterNamesCriteria.NJUGADODRES);


             /*
             *   Inserciones de las clausulas where        
             */  

             //Hacemos un where para buscar por id de producto
             if (productoCriteria.getIdProducto()!=null)
                 criteria.where(builder.equal(producto.get(ParameterNamesCriteria.PID), productoCriteria.getIdProducto()));

             //Hacemos un where para buscar por nombre
             if (productoCriteria.getNombre()!=null)  
                 builder.equal(producto.get(ParameterNamesCriteria.PNOMBRE), "%"+productoCriteria.getNombre()+"%");

             //Hacemos un where para buscar por año
             if (productoCriteria.getAnio()!=null) 
                 builder.equal(producto.get(ParameterNamesCriteria.PANIO), productoCriteria.getAnio());

             //Hacemos un where para buscar por precio
             if (productoCriteria.getPrecio()!=null)
                 criteria.where(builder.equal(producto.get(ParameterNamesCriteria.PPRECIO), productoCriteria.getPrecio()));

//           //Hacemos un where para buscar por oferta
//           if (productoCriteria.getOferta()!=null) 
//               criteria.select(producto).where(builder.equal(ofertaJoin.get(ParameterNamesCriteria.OID), productoCriteria.getOferta()));
//           
//           //Hacemos un where para buscar por categoria
//           if (!productoCriteria.getCategorias().isEmpty())            
//               criteria.select(producto).where(builder.equal(categoriaJoin.get(ParameterNamesCriteria.CID), productoCriteria.getCategorias()));

//           //Hacemos un where para buscar por njugadores
//           if (!productoCriteria.getNjugadores().isEmpty()) 
//               criteria.select(producto).where(builder.equal(jugadoresJoin.get(ParameterNamesCriteria.INJ), productoCriteria.getNjugadores()));
//           
//           //Hacemos un where para buscar por idioma
//           if (!productoCriteria.getIdioma().isEmpty()) 
//               criteria.select(producto).where(builder.equal(idiomaJoin.get(ParameterNamesCriteria.IID), idioma));


//           //Hacemos un where para internazionalizar la búsqueda
//           if (idioma!=null) 
//               criteria.select(producto).where(builder.equal(idiomaJoin.get(ParameterNamesCriteria.IID), idioma));
//           
             TypedQuery<Producto> mostrar = session.createQuery(criteria);


             List<Producto> productos=mostrar.getResultList();

//          if (idioma!=null) {
//              addClause(queryString, first, " pi.id_idioma LIKE ? ");
//              first = false;
//          }

            return productos;

            } finally {
                session.close();
        }
    }

My problem appears when I try to use 2 or more "restrictions" to do a specific search. In this scenario if I introduce 2 or more values to the product (For example, idProducto and anio(Release year of the game)), my criteria wont append this two restrictions, executing the sql query using only the last where that I use, in this case, "anio".

So I am asking how can, at the same time, check if a parameter that I receive is null or not and in the second case, append the criteria.select(producto).where(builder.equal.. to my sql query.

Thank you a lot for your time.

Try creating Predicate and adding them to the criteria: Predicate condition = builder.equal(...); criteria.add(condition); – Fran Montero 23 hours ago

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM