hibernate报错:
17:30| ERROR | ErrorCounter.java 56| Unable to locate appropriateconstructor on class [entity.NewsBean]
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: entity.NewsBean]
原因:
是实体类那里没有这个构造方法,提取什么字段就要这个构造方法。
解决方法:
在实体类里面添加这个构造方法,即可解决问题。
测试类代码:
@Test
public void testSelectMySelf(){
Stringhql="select newNewsBean(n.newsid,n.newstitle,n.author) from NewsBean as n";
Queryquery=session.createQuery(hql);//import org.hibernate.Query
List<NewsBean>list=query.list();
for(NewsBeannewsBean:list){
System.out.println("自定义id:"+newsBean.getNewsid());
System.out.println("自定义newstitle:"+newsBean.getNewstitle());
System.out.println("自定义author:"+newsBean.getAuthor());//通过别名来获取
}
}
NewsBean实体类必须添加构造函数代码,否则报错:
public NewsBean(intnewsid, String newstitle, String author) {
super();
this.newsid = newsid;
this.newstitle = newstitle;
this.author = author;
}
完成后重新运行,即可成功地解决了问题。