not-null property references a null or transient value: 等关于cinema项目bug

本文详细介绍了在项目开发过程中遇到的一些常见Bug及其解决方案,包括不为空属性引用的问题、JDBC批量更新失败的原因、SQL语句拼接错误及动态SQL拼接的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天改一个项目,遇到好多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等赋值时,必须为属性。不能随便写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值