I have the following objects:
@Entity
@Table(name = "place_revision")
public class PoiRevision {
@OneToMany(mappedBy = "pk.revision", cascade = {CascadeType.ALL})
private Collection<PoiRevisionCategory> categoryMapping;
}
@Entity
@Table(name = "place_revision__category")
@AssociationOverrides({
@AssociationOverride(name = "pk.revision",
joinColumns = @JoinColumn(name = "place_revision_id")),
@AssociationOverride(name = "pk.category",
joinColumns = @JoinColumn(name = "category_id"))
})
public class PoiRevisionCategory {
@EmbeddedId
private PoiRevisionCategoryId pk = new PoiRevisionCategoryId();
}
@Embeddable
public class PoiRevisionCategoryId implements Serializable {
@ManyToOne
private PoiRevision revision;
@ManyToOne
private Category category;
}
@Entity
@Table(name = "category")
public class Category {
@ManyToMany(targetEntity = Section.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinTable(
name = "category__section",
joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "section_id")
)
private Collection<Section> sections;
}
And you want to choose PoiRevisionswhich ones Categorieshave Sections. I am using Spring -data Specificationto query the database for these objects.
I intend to write something like:
Specification<PoiRevision> spec = new Specification<PoiRevision>() {
@Override
public Predicate toPredicate(Root<PoiRevision> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> conditions = new ArrayList<>(CONDITION_COUNT);
CollectionJoin<PoiRevision, PoiRevisionCategory> mapping = root.join(PoiRevision_.categoryMapping);
Join<PoiRevisionCategory, Category> categories = mapping.join("pk.category");
conditions.add(categories.get("sections").in(sections));
return cb.and(conditions.toArray(new Predicate[] {}));
}
};
But we cannot use the nested path for unions like the JPA provider (Hibernate, in my case) searches only for the direct properties of the class PoiRevisionCategory. And we cannot “attach” the inline identifier to our result set because it is not a managed entity.
I am really stuck with this problem, which seems to be far from complicated when switching to SQL, but it has some complexity on the ORM side.
Any idea is much appreciated.