ClassCastException for JPA query that tries to return the object queried in an inner SELECT statement

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật