结果集中的对象被转换为“对象”,而不是我在@SQLResultSetMapping 对象中指定的对象。
我正在尝试获取 ConstructorResult 的句柄,并创建了一个包含简单连接的查询,并尝试获取结果集并循环,尽管它打印出来以确保我做对了。但是,当我进入循环时,看起来应该是直截了当的。
当我声明结果列表时,它被强制转换为 type 。我单步执行查询测试类,它成功运行查询并将其加载到结果中,但结果列表中的项目键入为“对象”而不是 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 对象。然而,正确的信息在对象中。