0

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

2 に答える 2

0

そのフィールドがタイムスタンプの日付と時刻の場合は、(日付 00:00:00 と日付 23:59:59) の間のすべてのデータをクエリするようにクエリを変更して、必要なすべてのデータを取得する必要があります。

于 2013-08-14T08:57:14.347 に答える
0

この場合、Hibernate フィルターで HQL 名前付きクエリを使用します。 http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/filters.html

親コードは次のようになります。

  @OneToMany( cascade = {CascadeType.ALL},mappedBy = "parentId",fetch = FetchType.LAZY)
  [...]
  @Filter(name = "todayChildrenOnly", condition = "date = :now")
  private List<Child> children=new ArrayList<Child>();

上記は、SQL 結合条件および外部キー チェック (join ... on ...) で変換されます。「日付」が子テーブルのインデックスである場合、パフォーマンスが向上するはずです。

于 2013-08-14T09:23:16.700 に答える