JPA 2.1 ConstructorResult导致ClassCastException

我的结果集中的对象被强制转换为’Object’而不是我在@SQLResultSetMapping对象中指定的对象。

我正在尝试获取ConstructorResult的句柄,并创建了一个包含简单连接的查询,并尝试获取结果集并循环,但它将其打印出来以确保我正确。 然而,当我进入循环时,看起来应该是直截了当的不是。

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

代码发布在下面…我截断了一些列名称只是为了缩短它们。 如果它有助于将其添加回来让我知道。

public List 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 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的约束:

INFO:绑定结果集映射: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; } 

最后是一个来自调试器的screengrab,它将结果集显示为Objects而不是CommentInfoListItemDTO对象。 然而,正确的信息在对象中。

最后是一个来自调试器的screengrab,它将结果集显示为Objects而不是CommentInfoListItemDTO对象。

java.lang.ClassCastException:[Ljava.lang.Object; 无法投射到YourDTO

当EclipseLink找不到传递给你的YourDTO名称时YourDTO

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

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

 org.hibernate.MappingException: Unknown SqlResultSetMapping [someNonExistingMappingName] 

您必须确保您的持久性提供程序注册了YourDTO 怎么样? 观察您的日志:

hibernate:

DEBUG annotations.ResultsetMappingSecondPass – 绑定结果集映射:YourDTO

EclipseLink:我没有找到任何日志。

对映射的另一个注意事项:对于模糊类型使用@ColumnResult类型:

 @ColumnResult(name = "article_publish_date", type=Calendar.class) @ColumnResult(name = "article_id", type=BigInteger.class)