标题问题出现的可能比较多,现在将原因及解决办法汇总如下:
例子1: 因为实体类中id的属性和hbm.xml配置文件中id的属性不一致造成的
实体类中的id属性是:
- public class ThreadPushFail {
- private long id;
-
-
-
- private long pushMessageId;
hbm.xml配置文件中id的属性:
- <hibernate-mapping>
- <class name="com.tdr.push.entity.ThreadPushFail" table="t_threadPushFail" lazy="true">
- <id name="id" type="int">
- <column name="ID" precision="19" scale="0">
- <comment>主键id</comment>
- </column>
- <generator class="identity"/>
- </id>
一个是int,一个是long 解决方案:是id的属性保持一致。 |
例子2:对象复制时导致,因为hibernate的sessionFactory().getSession中不能存在2个相同id的对象
用BeanUtils.copyProperties(B,A)把A对象的所有属性都copy的B对象中 并把B的主键setId(null)放空掉 |