org.hibernate.HibernateException:identifier of an instance of xxx(实体类) was altered from xxx to xxxPK

检查Project类的主键属性是否是封装类型。例如:Integer,Long ……。不能是int,long等数据类型。

若Dao层调用 Hibernate框架 的 saveOrupdate()函数报错,若实体类有多个主键,并且改变了主键,此时不可使用updateEntity(),需先delete表中旧数据再直接saveEntity()新数据。 此时若只改变了非主键,进行 delete、save也可能报错,这时可直接使用updateEntity()

//dao层

//add

public Project addholiday(Project p){

return this.saveEntity(p);}

//update (只改变来了非主键)

public void updateHoliday(Project p){

this.updateEntity(p);

}

//delete

public int delholiday(Project p){

/* 你自己的实体类

String hql = "DELETE FROM ActHolidayshiftday " +

" WHERE year = '" + holiday.getYear() + "' " +

" and month = '" + holiday.getMonth() + "' " +

" and day = '" + holiday.getDay() + "' " +

" and holidayName ='" + holiday.getHolidayName() + "'";

*/

return this.getHibernateTemplate().bulkUpdate(hql);

}

//edit (实体类有多主键,改变了主键)

public void editHoliday(Project oldp, Project newp){

this.delholiday(oldp);

this.saveEntity(newp);

}

好文推荐

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: