Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在hibernate/Spring中的oneToMany映射表上添加筛选器_Spring_Hibernate_Hibernate Mapping - Fatal编程技术网

在hibernate/Spring中的oneToMany映射表上添加筛选器

在hibernate/Spring中的oneToMany映射表上添加筛选器,spring,hibernate,hibernate-mapping,Spring,Hibernate,Hibernate Mapping,我在当前项目中使用hibernate4和spring3.1 我有两个具有一对多映射的表。我正在使用基于注释的映射。 映射如下所示: public class TableA { @Id private Long id; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "sample_id") private S

我在当前项目中使用hibernate4和spring3.1

我有两个具有一对多映射的表。我正在使用基于注释的映射。 映射如下所示:

public class TableA {
       @Id
       private Long  id;

       @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
       @JoinColumn(name = "sample_id")
       private Set<TableB> tableBList;

       // setter getter
   }


   public class TableB{   
       @Id
       private Long                  id;

       private Long                  sample_id;

       private Date added_date;
   }
它将返回表B中为id=5映射的所有行。 但是我想再添加一个条件,比如
added\u date=XXXX


是否可以在查询中或通过任何其他方式在“添加日期”列上添加筛选器?

是,可以通过查询:

请参阅hql文档:

你想要什么

String hql = "select a from TableA as a inner join a.tableBList as b WHERE a.id = :id and b.added_date > :after";
Query q = sessionFactory.getCurrentSession().createQuery(hql);
q.setParameter("id", 5);
q.setParameter("after", date);
q.list();

CriteriaAPI是处理许多条件的最好方法。通过这个查询,我得到了两个不同列表的集合。一个包含表B中的所有数据,另一个包含唯一所需列表的列表。但我只想检索所需列表。请阅读文档:第16.6节:select子句。添加从表格中选择一个。。。更新了我的答案
String hql = "select a from TableA as a inner join a.tableBList as b WHERE a.id = :id and b.added_date > :after";
Query q = sessionFactory.getCurrentSession().createQuery(hql);
q.setParameter("id", 5);
q.setParameter("after", date);
q.list();