再使用HQL語句通過封裝查詢時,會添加一個虛擬的實體類例如
select new com.musicworld.vo.ComUserSong(c.commentId,c.commentTime,c.content,c.userinfo.userName) from Comment c
這種封裝需要實體類提供對應構造器
構造方法
public ComUserSong(Integer commentId, Date commentTime, String content,
String userName) {
super();
this.commentId = commentId;
this.commentTime = commentTime;
this.content = content;
this.userName = userName;
}
但使用中經常會拋這樣的異常:Unable to locate appropriate constructor on class。
出現這個異常需要檢查以下幾種情況:
1)參數構造器的參數類型是否正確
2)參數構造器的順序和hql中的順序是否一致
3)參數構造器的參數個數是否和hql中的個數一致
4)參數構造器的參數類型是否TimeStamp