2013-02-15 62 views
4

需要知道如何构建一个Hibernate查询,该查询获取匹配包含多个列值的IN子句的结果。具有多列的Hibernate IN子句

例如,

Query query=session.createQuery("from entity e where (e.abc, e.xyz) in (:list)"); 
query.setParameterList("list", list); 

list这里将2D阵列可能包含基本包装对象为原始类型例如IntegerString

这是可能的?

回答

4

放下这里我是如何实现这一点。基本上,我们需要从我们需要查询的一组列中创建一个Hibernate组件(读取@Embeddable对象)并将其嵌入到主实体中。

的组的列的可如下组合:

@Entity 
public class MyEntity{ 
@Id 
private Integer id; 
private String col3; 
private String col4 
@Embedded 
private CompositeColumns pairedCol1Col2; 
... 
... 
//Getters Setters 

} 


@Embeddable 
public class CompositeColumns{ 
    private String col1; 
    private String col2; 

    //Empty constructor is required by Hibernate for instantiation 
    public CompositeColumns(){ 
    } 

    public CompositeColumns(String col1, String col2){ 
      this.col1 = col1; 
      this.col2 = col2; 
     } 

    @Column(name="COL1") 
    public String getCol1(){ 
    } 
    ... 
    ... 
    //Rest of getters and setters 
} 



嵌入在如下的主要实体类以上


查询会再看看如下:

List<CompositeColumns> cols = //get a list of CompositeColumns type 

Query query=session.createQuery("from MyEntity where pairedCol1Col2 in (:list)"); 
query.setParameterList("list", list); 


这做这项工作。

注:我跑这一个Oracle数据库