简体   繁体   English

Hibernate Criteria 多个 where

[英]Hibernate Criteria multiple where

Good morning, I am trying to create a method to return several games from my MySQL DB using Hibernate Criteria.早上好,我正在尝试创建一种方法,使用 Hibernate Criteria 从我的 MySQL 数据库返回多个游戏。 The method receives a Product object which can have multiple parameters at null.该方法接收一个 Product 对象,该对象可以有多个 null 参数。 I'm working on Hibernate 5.2 right now.我现在正在研究 Hibernate 5.2。

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:这是 Hibernate Criteria 代码:

@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.当我尝试使用 2 个或更多“限制”进行特定搜索时,我的问题出现了。 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".在这种情况下,如果我向产品引入 2 个或更多值(例如,idProducto 和 anio(游戏发行年份)),我的条件不会附加这两个限制,仅使用我使用的最后一个位置执行 sql 查询,在这种情况下,“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.所以我问如何同时检查我收到的参数是否为空,在第二种情况下,将criteria.select(producto).where(builder.equal..附加到我的 sql 查询中。

Thank you a lot for your time.非常感谢您的宝贵时间。

Try creating Predicate and adding them to the criteria: Predicate condition = builder.equal(...);尝试创建 Predicate 并将它们添加到条件中: Predicate condition = builder.equal(...); criteria.add(condition); criteria.add(条件); – Fran Montero 23 hours ago – Fran Montero 23 小时前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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