再使用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