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

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

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

该错误通常是由于在实体类中定义为非空(not-null)的属性,在使用时却为 null 或临时值所导致。以下是几种可能的解决方法: ### 为 cuisineId 赋值 需要确保在使用 `Dish` 实体类时,为 `cuisineId` 属性赋予一个有效的值。示例代码如下: ```java import com.foodsystem.Entity.Pojo.Dish; public class Main { public static void main(String[] args) { Dish dish = new Dish(); // 为 cuisineId 赋值 dish.setCuisineId(1L); } } ``` ### 检查映射文件 若使用了映射文件(如 XML 映射文件),需要检查 `cuisineId` 属性的映射配置,确保没有错误地将其设置为非空。示例的 XML 映射配置如下: ```xml <class name="com.foodsystem.Entity.Pojo.Dish" table="dish"> <!-- 其他属性映射 --> <property name="cuisineId" type="java.lang.Long"> <column name="cuisine_id" not-null="false"/> <!-- 确保 not-null 为 false --> </property> </class> ``` ### 检查数据库约束 要保证数据库表中 `cuisine_id` 列没有被设置为非空约束。若不需要该列非空,可以通过 SQL 语句修改表结构: ```sql ALTER TABLE dish MODIFY cuisine_id BIGINT NULL; ``` ### 单元测试验证 编写单元测试来验证 `Dish` 实体类的使用是否正确,确保 `cuisineId` 能够正确赋值。示例代码如下: ```java import com.foodsystem.Entity.Pojo.Dish; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; public class DishTest { @Test public void testDishCuisineId() { Dish dish = new Dish(); dish.setCuisineId(1L); assertNotNull(dish.getCuisineId()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值