この条件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;