简体   繁体   中英

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

CASE: I've got 2 tables with following OneToMany relationship: TagAbstract 1..* TagConf , where:

  • tag_abstract_ID is a field identifying many TagConf records that are historical configurations of single tag object,
  • the "active configuration" is recognized as one with valid_until = NULL ,
  • active tags are ones having latest configuration active.

DB schema is:

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
);

QUESTION: Is it possible to define Hibernate/JPA query (maybe HQL) with criteria for these TagAbstract records, where last of referenced TagConf "is active" (as defied above)?

I've stuck here:

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();
}

For this sample data it should only select records 1 and 2 :

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

The data mapping is defined as follows:

@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;
    }
}

So I've corrected the mapping according to JB Nizet suggestion in comment above - added @ManyToOne mapping to 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;
    }
}

and it works...

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

Thanks JB Nizet !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM