天天看点

Hibernate执行查询后又立马执行了更新(default-access)

在项目中遇到利用hibernate执行查询后立马又对数据库数据进行了更新,导致了错误。
public String getName() 
{   
    if(this.name==null)
    {   
        this.name="";   
    }   
    return this.name;   
}  
      

因为在数据库中是null,在hibernate查询时会设置成“”。hibernate会任务session中的实体发生了改变,就会执行update。

解决方式: 

 采用的解决办法是将配置文件增加一个属性access="field" 这样就不会通过get方法比较属性而直接访问属性字段

<property name="name" type="java.lang.String" access="field">

  <column name="name" length="20" />

 </property>

default-access="field ¦property ¦ClassName"