I have the following JPA query that attempts to find all the Children of a Parent object that fit under certain conditions, and then get only the first one of them under different partitions:
@Repository
public interface MyRepository {
@Query("SELECT sorted_children.child_1 " +
" FROM ( " +
" SELECT child AS child_1, " +
" ROW_NUMBER() OVER (PARTITION BY child.field2, child.parent.field2 " +
" ORDER BY child.parent.field1 ASC) AS rowNum " +
" FROM ChildObject child " +
" WHERE child.field1 IN (?1) AND " +
" child.parent.field1 IN (?2) " +
" ) AS sorted_children" +
" WHERE sorted_children.rowNum = 1")
Set<ChildObject> findChildObjects(Set<Long> childField1, Set<Long> parentField1);
}
But for some reason I’m getting the following error:
java.lang.ClassCastException: class org.hibernate.persister.entity.SingleTableEntityPersister cannot be cast to class org.hibernate.metamodel.mapping.EntityAssociationMapping
(org.hibernate.persister.entity.SingleTableEntityPersister and org.hibernate.metamodel.mapping.EntityAssociationMapping are in unnamed module of loader 'app')
The Entity Objects looks like the following.
Parent:
@Entity
@Table(name = "parent_table", schema = "schema")
public class ParentObject {
@Id
@Column(name = "id")
public Long id;
@Column(name = "field1")
public Long field1;
@Column(name = "field2")
public String field2;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
public Set<ChildObject> children;
}
Child:
@Entity
@Table(name = "child_table", schema = "schema")
public class ChildObject {
@Id
@Column(name = "id")
public Long id;
@Column(name = "field1")
public Long field1;
@Column(name = "field2")
public String field2;
@ManyToOne
@JoinColumn(name = "parent_id", nullable = false, updatable = false)
public ParentObject parent;
}
I’ve tried some different queries that work, but they don’t return the results I’m looking for.
Example #1 (not the first nor partitioned):
@Query("SELECT child " +
" FROM ChildObject child " +
" WHERE child.field1 IN (?1)")
Set<ChildObject> findChildObjects(Set<Long> field1);
Example #2 (I’m getting the parent objects, not the children):
@Query("SELECT sorted_children.parent " +
" FROM ( " +
" SELECT child.parent AS parent, " +
" ROW_NUMBER() OVER (PARTITION BY child.field2, child.parent.field2 " +
" ORDER BY child.parent.field1 ASC) AS rowNum " +
" FROM ChildObject child " +
" WHERE child.field1 IN (?1) AND " +
" child.parent.field1 IN (?2) " +
" ) AS sorted_children" +
" WHERE sorted_children.rowNum = 1")
Set<ParentObject> findParentObjects(Set<Long> childField1, Set<Long> parentField1);
I’m a bit surprised the example #2 works, because it is quite similar in the sense that the SELECT
statement returns the object found in the inner query (which I understand that internally it is translated to a CTE (Common Table Expression) by Hibernate). So, my best guess is that there is something special that needs to be done with the ChildObject
to be able to return it as part of an operation as it is currently being identified as a SingleTableEntityPersister
, and Hibernate expects a EntityAssociationMapping
.
Therefore, my question are:
- What am I doing wrong in my original query to get a
ClassCastException
? - How can I make my query succeed?
I still have to find a satisfying explanation, but doing the following was good enough to get the results I was looking for:
@Repository
public interface MyRepository {
@Query("SELECT child " +
" FROM ( " +
" SELECT child.id AS child_id, " +
" ROW_NUMBER() OVER (PARTITION BY child.field2, child.parent.field2 " +
" ORDER BY child.parent.field1 ASC) AS rowNum " +
" FROM ChildObject child " +
" WHERE child.field1 IN (?1) AND " +
" child.parent.field1 IN (?2) " +
" ) AS sorted_children" +
" INNER JOIN ChildObject child ON child.id = sorted_children.child_id" +
" WHERE sorted_children.rowNum = 1")
Set<ChildObject> findChildObjects(Set<Long> childField1, Set<Long> parentField1);
}
Basically, instead of returning the child
object in the inner query, I’m just returning the id
, so that I can later get the whole ChildObject
.
It is not the most elegant nor the best performing query, but it gets the job done for my purposes and constraints.
I suppose my original query was returning some sort of projection of the ChildObject
that wasn’t suitable to rebuild the original ChildObject
entity. Maybe because the rowNum
field was among them as part of the SELECT
? In any case, doing the INNER JOIN
to re-query for ChildObject
helped Hibernate to understand that the ChildObject
entity was to be returned.