JPA 2.1 ConstructorResult 导致 ClassCastException

2024-03-30

结果集中的对象被转换为“对象”,而不是我在 @SQLResultSetMapping 对象中指定的对象。

我正在尝试获取 ConstructorResult 的句柄,并创建了一个包含简单联接的查询,并尝试获取结果集并循环打印它,以确保它是正确的。然而,当我进入循环时,看起来应该很简单的事情却并非如此。

当我声明结果列表时,它被强制转换为类型。我单步执行查询测试类,它成功运行查询并将其加载到结果中,但结果列表中的项目已键入“对象”而不是 CommentInfoListItemDTO 对象。因此,当我进入循环时,它会遇到类转换异常。为什么结果不转换为 CommentInfoListItemDTO 对象?特别是当@SQLResultSetMapping 中指定了这一点时。

代码发布在下面...我截断了一些列名称只是为了缩短它们。如果将其添加回来有帮助,请告诉我。

public List<CommentInfoListItemDTO> getCommentTitleListByPersonId(BigInteger personId) {
    String queryString = "select c.article_id, "
                        ***[columns removed for brevity]***
                        + "c.person_id as comment_person_id, "
                        + "a.party_id as aticle_party_id "
                        + "from article_comment c "
                        + "join article a "
                        + "on a.article_id = c.article_id "
                        + "where c.person_id = :personId";

    Query q = em.createNativeQuery(queryString, "CommentInfoListItemDTOMapping");
    q.setParameter("personId", personId);

    List<CommentInfoListItemDTO> commentInfoList = q.getResultList();

    ***[throws exception on the next line]***
    ***[java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com...CommentInfoListItemDTO]***

    for (CommentInfoListItemDTO listElement : commentInfoList){
            System.out.println("COMMENT TITLE LIST: " + listElement.toString());
        }
    return (commentInfoList);
}

@SqlResultSetMapping——

编辑——展示如何将其放置在现有实体类中。

编辑 - 我尝试将数据类型添加到映射中的列列表中。这没有帮助。结果集中的每条记录仍然被转换为 Hibernate 内某处的 java.lang.Object,并且它不会让我将其转换回 DTO。结果集映射受 Hibernate 约束:

信息:绑定结果集映射:CommentInfoListItemDTOMapping

@Entity
@SqlResultSetMapping(name = "CommentInfoListItemDTOMapping", classes = {
    @ConstructorResult(targetClass = CommentInfoListItemDTO.class,
            columns = {
            @ColumnResult(name = "article_id", type=BigInteger.class),
            @ColumnResult(name = "article_comment_id", type=BigInteger.class),
            @ColumnResult(name = "parent_comment_id", type=BigInteger.class),
            @ColumnResult(name = "article_title", type=String.class),
            @ColumnResult(name = "article_published", type=Boolean.class),
            @ColumnResult(name = "article_publish_date", type=Calendar.class),
            @ColumnResult(name = "article_availability_state", type=String.class),
            @ColumnResult(name = "article_enable_comments", type=Boolean.class),
            @ColumnResult(name = "comment_title", type=String.class),
            @ColumnResult(name = "comment_hide", type=Boolean.class),
            @ColumnResult(name = "comment_created_timestamp", type=Calendar.class),
            @ColumnResult(name = "comment_person_id", type=BigInteger.class),
            @ColumnResult(name = "aticle_party_id", type=BigInteger.class)
            })
})

@Table(name = "article_comment")
@NamedQueries({
    @NamedQuery(name = "ArticleComment.findAll", query = "SELECT e FROM ArticleComment e")})

public class ArticleComment implements Serializable {
...

POJO

public class CommentInfoListItemDTO implements Serializable {

    private Integer id;
    private BigInteger articleId;
    private BigInteger articleCommentId;
    private BigInteger parentCommentId;
    private String articleTitle;
    private Boolean articlePublished;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Calendar articlePublishDate;
    private String articleAvailabilityState;
    private Boolean articleEnableComments;
    private String commentTitle;
    private Boolean commentHide;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Calendar commentCreatedTimestamp;
    private BigInteger commentPersonId;
    private BigInteger articlePartyId;

    public CommentInfoListItemDTO() {
    }

    public CommentInfoListItemDTO(BigInteger articleId, BigInteger articleCommentId, 
            BigInteger parentCommentId, String articleTitle, Boolean articlePublished, 
            Calendar articlePublishDate, String articleAvailabilityState, 
            Boolean articleEnableComments, String commentTitle, Boolean commentHide, 
            Calendar commentCreatedTimestamp, BigInteger commentPersonId, 
            BigInteger articlePartyId) {
        this.articleId = articleId;
        this.articleCommentId = articleCommentId;
        this.parentCommentId = parentCommentId;
        this.articleTitle = articleTitle;
        this.articlePublished = articlePublished;
        this.articlePublishDate = articlePublishDate;
        this.articleAvailabilityState = articleAvailabilityState;
        this.articleEnableComments = articleEnableComments;
        this.commentTitle = commentTitle;
        this.commentHide = commentHide;
        this.commentCreatedTimestamp = commentCreatedTimestamp;
        this.commentPersonId = commentPersonId;
        this.articlePartyId = articlePartyId;
    }

最后是调试器的屏幕截图,将结果集显示为对象而不是 CommentInfoListItemDTO 对象。然而,正确的信息位于对象中。


java.lang.ClassCastException:[Ljava.lang.Object;无法转换为 YourDTO

当 EclipseLink 找不到名称时抛出YourDTO传递给

em.createNativeQuery("SELECT...","YourDTO");

Hibernate在类似情况下抛出的异常是:

org.hibernate.MappingException: Unknown SqlResultSetMapping [someNonExistingMappingName]

你必须确保YourDTO由您的持久性提供商注册。如何?观察你的日志:

休眠:

DEBUG 注解.ResultsetMappingSecondPass - 绑定结果集映射:YourDTO

Eclipse链接: 我没有找到任何相关日志。

对您的映射的另一项注释:使用@ColumnResult with type对于不明确的类型:

@ColumnResult(name = "article_publish_date", type=Calendar.class)
@ColumnResult(name = "article_id", type=BigInteger.class)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JPA 2.1 ConstructorResult 导致 ClassCastException 的相关文章

随机推荐