我的结果集中的对象被强制转换为'对象'而不是我在@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约束:
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对象。然而,正确的信息在对象中。
答案 0 :(得分:0)
当EclipseLink找不到传递给的java.lang.ClassCastException:[Ljava.lang.Object;无法投射到YourDTO
YourDTO
名称时,会抛出
em.createNativeQuery("SELECT...","YourDTO");
Hibernate在类似情况下引发的异常是:
org.hibernate.MappingException: Unknown SqlResultSetMapping [someNonExistingMappingName]
您必须确保持久性提供商注册YourDTO
。如何?观察您的日志:
休眠:
DEBUG annotations.ResultsetMappingSecondPass - 绑定结果集映射:YourDTO
的EclipseLink: 我没有找到任何日志。
对您的地图进行另外一次评论:将@ColumnResult
与type
一起用于不明确的类型:
@ColumnResult(name = "article_publish_date", type=Calendar.class)
@ColumnResult(name = "article_id", type=BigInteger.class)