我想实现一个查询,它选择所有满足这个条件的只有孩子的父母children.date = today。目标是提高性能,我不需要其他日子的孩子。
Query q = ss.createQuery("select p from Parent p where exists( from p.children c where c.date = :now)");
q.setTimestamp("now", new DateTime().minusDays(1).toDate());
qc=q.list();
另一种方式 :
DateMidnight first = new DateMidnight(); //today at 00h00 00min 00 JodaTime
ss.createCriteria(Parent.class)
.createAlias("children", "c")
.add(Restrictions.between("c.date",first.toDate(), first.plus(14400000).toDate()))
.list();
家长班
@Entity
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private Integer id;
@OneToMany( cascade = {CascadeType.ALL},mappedBy = "parentId",fetch = FetchType.LAZY)
@Column(name="id_parent",nullable=true,unique = true)
@Cascade(value={org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE,org.hibernate.annotations.CascadeType.REMOVE})
private List<Child> children=new ArrayList<Child>();
和 Child.class
@Entity
public class Child implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@Column(name = "childID", unique = true, nullable = false)
private long childId1;
private Date date;
private String childId2;