Hibernate IN子句有多列 [英] Hibernate IN Clause with multiple columns

查看:104
本文介绍了Hibernate IN子句有多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<例如,

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

这里 list 是一个2D数组,可以包含原始类型的基本包装器对象,例如 Integer String 等。



这是可能的吗?

解决方案

基本上,我们需要在我们需要查询的一组列中创建一个Hibernate组件(读@Embeddable对象),并将它嵌入到主实体中。



组可以合并如下:

  @Embeddable 
public class CompositeColumns {
private String col1;
私人字符串col2;

// Hibernate需要空构造函数进行实例化
public CompositeColumns(){
}

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


@Column(name =COL1)
public String getCol1(){
}
...
.. 。
//其余的getter和setters
}







将以上内容嵌入您的主实体类中,如下所示:

  @Entity 
public class MyEntity {
@Id
private Integer id;
私人字符串col3;
私人字符串col4
@Embedded
私人CompositeColumns pairedCol1Col2;
...
...
// Getters Setters

}







查询如下所示:

  List< CompositeColumns> cols = //获取CompositeColumns类型的列表

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





这样做。



注意:我在Oracle数据库上运行它


Need to know how to construct a Hibernate Query which fetches results matching an IN clause containing multiple column values.

e.g.,

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

Here list would be a 2D array that could contain basic wrapper objects for primitive types e.g., Integer, String etc.

Is this possible?

解决方案

Putting down here how I implemented this. Basically we need to make a Hibernate Component (read @Embeddable object) out of the set of columns we need to query on and embed it in the main Entity.

The group of columns can be combined as below:

@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
}



Embed the above in your main entity class as below:

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

}



The query would then look as below:

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

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


This does the job.

Note: I ran this on an Oracle database

这篇关于Hibernate IN子句有多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆