簡體   English   中英

Hibernate / JPA查詢,其中包含列表字段中元素的條件(稱為OneToMany)

[英]Hibernate/JPA query with criteria for elements in list field (referenced as OneToMany)

案例:我有2個具有以下OneToMany關系的表: TagAbstract 1..* TagConf ,其中:

  • tag_abstract_ID是一個字段,用於標識許多TagConf記錄,這些記錄是單個標簽對象的歷史配置,
  • 使用“ valid_until = NULL ”將“活動配置”識別為一個,
  • 活動標簽是具有最新配置活動的標簽。

數據庫模式為:

CREATE TABLE TagAbstract (
  tag_abstract_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,

  PRIMARY KEY (tag_abstract_ID)
);

CREATE TABLE TagConf (
  tag_conf_ID          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  tag_abstract_ID      INT UNSIGNED NOT NULL,
  valid_until          DATETIME,

  PRIMARY KEY (tag_conf_ID),
  FOREIGN KEY (tag_abstract_ID) REFERENCES TagAbstract (tag_abstract_ID)
    ON DELETE CASCADE
    ON UPDATE CASCADE
);

問題:是否可以使用這些TagAbstract記錄的條件來定義Hibernate / JPA查詢(可能是HQL),其中引用的TagConf的最后一個“處於活動狀態”(如上所述)?

我被困在這里:

public List<TagAbstract> fetchTagAbstractActive() {
    //noinspection unchecked

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();

    CriteriaQuery<TagAbstract> criteria = builder.createQuery(TagAbstract.class);
    Root<TagAbstract> tagAbstractRoot = criteria.from(TagAbstract.class);
    criteria.select(tagAbstractRoot);

    // TODO: how to correctly define following criterion in Hibernate/JPA query?
    // ---------------------------------------------------------------------
    Predicate predicate = ?; // for tagConfigs[last].validUntil == NULL
    // ---------------------------------------------------------------------

    criteria.where(predicate);
    return entityManager.createQuery(criteria).getResultList();
}

對於此樣本數據,它應該僅選擇記錄12

record 0   
    tagConfigs  
        ref 0   
            tagConfId      1
            tagAbstractId  1
            validUntil     "11-08-2017 08:11:45"

record 1   
    tagConfigs  
        ref 0   
            tagConfId      2
            tagAbstractId  2
            validUntil     "11-08-2017 08:19:19"
        ref 1   
            tagConfId      4
            tagAbstractId  2
            validUntil     NULL

record 2   
    tagConfigs  
        ref 0   
            tagConfId      3
            tagAbstractId  3
            validUntil     NULL

數據映射定義如下:

@Entity
@Table(name = "TagAbstract")
public class TagAbstract {
    private static final long serialVersionUID = 1L;

    @Id()
    @GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sequence", allocationSize = 10)
    @Column(name = "tag_abstract_ID")
    private long tagAbstractId;

    @OneToMany(mappedBy = "tagAbstractId", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<TagConf> tagConfigs = new ArrayList<>();

    public List<TagConf> getTagConfigs() {
        return tagConfigs;
    }
}

@Entity
@Table(name = "TagConf")
public class TagConf {
    private static final long serialVersionUID = 1L;

    @Id()
    @GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sequence", allocationSize = 10)
    @Column(name = "tag_conf_ID")
    private long tagConfId;

    @Column(name = "tag_abstract_ID")
    private long tagAbstractId;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.DATE_TIME_PATTERN)
    @Column(name = "valid_until")
    private Date validUntil;

    public long getTagConfId() {
        return tagConfId;
    }

    public long getTagAbstractId() {
        return tagAbstractId;
    }

    public Date getValidUntil() {
        return validUntil;
    }
}

所以我按照上面的評論JB Nizet建議糾正映射-添加@ManyToOne映射到TagConf

@Entity
@Table(name = "TagAbstract")
public class TagAbstract {
    private static final long serialVersionUID = 1L;

    @Id()
    @GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sequence", allocationSize = 10)
    @Column(name = "tag_abstract_ID")
    private long tagAbstractId;

    @OneToMany(mappedBy = "tagAbstract", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<TagConf> tagConfigs = new ArrayList<>();

    public List<TagConf> getTagConfigs() {
        return tagConfigs;
    }
}

@Entity
@Table(name = "TagConf")
public class TagConf {
    private static final long serialVersionUID = 1L;

    @Id()
    @GeneratedValue(generator = "sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sequence", allocationSize = 10)
    @Column(name = "tag_conf_ID")
    private long tagConfId;

    @ManyToOne
    @JoinColumn(name = "tag_abstract_ID", foreignKey = @ForeignKey(name = "tag_abstract_ID"))
    private TagAbstract tagAbstract;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Const.DATE_TIME_PATTERN)
    @Column(name = "valid_until")
    private Date validUntil;

    public long getTagConfId() {
        return tagConfId;
    }

    public long getTagAbstractId() {
        return tagAbstract.getTagAbstractId();
    }

    public Date getValidUntil() {
        return validUntil;
    }
}

它有效...

    String hql = "select t from TagAbstract as t join t.tagConfigs tc where tc.validUntil is null";
    return (List<TagAbstract>) entityManager.createQuery(hql).getResultList();

謝謝JB Nizet

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM