今天改一个项目,遇到好多bug。以下一一说主要bug。
1.
not-null property references a null or transient value: entity.Film.name
很明显,意思是不能有一个空的属性引用。
解决:检查数据库,允许为空,发现还是报错。
在xml文件中,去掉属性中not-null即可。
2.Could not execute JDBC batch update
当然了一般都与inverse有关。
往后面拖动异常:还报了一个null。
说明update出错,记得用事务。不然数据库还是没有更新。
3.sql语句的拼接出错。
if (factor.length()>0) {
f.setProtagonist(factor);
}
if (fprice.length() > 0) {
f.setPrice(new Integer(fprice));
}
这里要注意了拼接属性时,要判断,同时要结合数据库中的列是否为空观察。
4.方法中,动态拼接sql。
错误的判断方法:
StringBuilder sb=new StringBuilder("from Film t where 1=1 ");
if (sfilm.getName()!=null) {
sb.append( "and t.name=:name");
}
if (sfilm.getFilmtype()!=null ) {
sb.append( " and t.filmtype.tid=:filmtype");
}
if (sfilm.getProtagonist()!=null ) {
sb.append(" and t.protagonist=:protagonist");
}
if (sfilm.getDirector()!=null ) {
sb.append(" and t.director=:director");
}
if (sfilm.getStartprice()!=null ) {
sb.append(" and t.price >= :startprice ");
}
if (sfilm.getEndprice() !=null) {
sb.append(" and t.price <= :endprice");
}
Query query=session.createQuery(sb.toString());
这时,错误出现了,如果,用类型赋值:
//query.setParameter("name", sfilm.getName());
// query.setParameter("filmtype", sfilm.getFilmtype());
// query.setParameter("protagonist", sfilm.getProtagonist());
// query.setParameter("director", sfilm.getDirector());
// query.setParameter("startprice", sfilm.getStartprice());
// query.setParameter("endprice", sfilm.getEndprice());
不论怎样,sql语句上面的if失效。
改成直接绑定对象传值:
query.setProperties(sfilm);
这样就可以获取集合数据了。
list=query.list();
注意了:使用setProperties时,name,filmtype等赋值时,必须为属性。不能随便写。