I seem to have a problem doing a query with criteria using an entity that has a bidirectional association (OneToMany) with another polymorphic entity (Using join inheritance).
The problem only appears if the association is bidirectional (declared in both, the container using mappedBy from the other one), if i declare it only in the container and the other knows nothing about the query works perfectly.
Maybe there is something wrong in my query but im finding it really hard to locate my problem.
I have created a small example, using the hibernate-test-case from github, that reproduces my problem.
I uploaded it to my one drive cant attach tar.gz here:
Test-Example
This is the simple model in the example:
This is the criteria i build:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<GroupContainerVariantA> query
= builder.createQuery(GroupContainerVariantA.class);
Root<GroupContainerVariantA> root
= query.from(GroupContainerVariantA.class);
Join join = root.join("elementSet" ,JoinType.INNER);
Path joinPath = builder.treat(join, ElementVariantA.class);
Path field = joinPath.get("fieldVA1");
Predicate predicate = builder.equal(field, "VA12");
query.select(root).distinct(true).where(predicate);
List<GroupContainerVariantA> result
= entityManager
.createQuery(query)
.getResultList();
// See if it worked
Assert.assertEquals(
"More results than expected",1,result.size());
Assert.assertEquals(
"Not the correct element",eva2.getId(),result.get(0).getId());
If i use the bidirectional association i get this strange exception (if i use single side from the container the query works and return the correct element as the test verifies)
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 3
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.getSubclassNameClosureBySubclassTable(JoinedSubclassEntityPersister.java:1051)
This is the data setup of the test:
GroupContainerVariantA gca = new GroupContainerVariantA();
gca.setFieldGCA1("GCA1");
ElementVariantA eva1 = new ElementVariantA();
eva1.setFieldVA1("VA11");
ElementVariantA eva2 = new ElementVariantA();
eva1.setFieldVA1("VA12");
ElementVariantB evb1 = new ElementVariantB();
evb1.setFieldVB1("VB11");
ElementVariantB evb2 = new ElementVariantB();
evb1.setFieldVB1("VB12");
gca.setElementSet(new HashSet<>(Arrays.asList(
eva1, eva2, evb1, evb2)));
entityManager.persist(gca);