many to one FK更新問題求解(identifier of an instance)
来源:互联网 发布:java object() 编辑:程序博客网 时间:2024/06/05 10:07
問題:
1.getSession().clear();//清除緩存
2.getHibernateTemplate().merge(object);
如果不加第一行,在更新FK時就會報下面的錯誤.這是為什麽?
錯誤如下:
Data Access Failure
identifier of an instance of com.tar.model.CargoType was alteredfrom 402881821bdb7471011bdb75c08e0002 to402881821bdb7471011bdb7586530001; nested exception isorg.hibernate.HibernateException: identifier of an instance ofcom.tar.model.CargoType was altered from402881821bdb7471011bdb75c08e0002 to 402881821bdb7471011bdb7586530001
我在論壇上看看別以前發的帖子,沒有找到答案,求解呀!
子類:
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import org.hibernate.annotations.GenericGenerator;
- public class Cargo extends BaseObject implements java.io.Serializable {
- private CargoType cargoType = new CargoType();
- @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
- @JoinColumn(name = "cargo_type_id")
- public CargoType getCargoType() {
- return this.cargoType;
- }
- public void setCargoType(CargoType cargoType) {
- this.cargoType = cargoType;
- }
- }
父類:
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import org.hibernate.annotations.GenericGenerator;
- public class CargoType extends BaseObject implements java.io.Serializable {
- private String cargoTypeId;
- private String cargoTypeName;
- private Set<Cargo> cargos = new HashSet<Cargo>(0);
- @Column(name = "cargo_type_name", nullable = false)
- public String getCargoTypeName() {
- return this.cargoTypeName;
- }
- public void setCargoTypeName(String cargoTypeName) {
- this.cargoTypeName = cargoTypeName;
- }
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cargoType")
- public Set<Cargo> getCargos() {
- return this.cargos;
- }
- public void setCargos(Set<Cargo> cargos) {
- this.cargos = cargos;
- }
- }
form jsp的內容如下:
- <form:select path="cargoType.cargoTypeId" cssClass="select medium" cssErrorClass="select medium error" id="cargoType.cargoTypeId" >
- <form:option value="" label="Please Select"/>
- <form:options items="${cargoTypeList}" itemValue="cargoTypeId" itemLabel="cargoTypeName"/>
- </form:select>
有沒有下面這兩行都是報那個錯誤.
執行刪除或更新其它字段都沒有問題.
- String cargoTypeId = request.getParameter("cargoType.cargoTypeId");
- cargo.setCargoType(cargoTypeManager.get(cargoTypeId));
- cargoManager.save(cargo);//調用的是這個方法getHibernateTemplate().merge(object)
问题补充
與以前一個帖子是相同的問題:
http://www.javaeye.com/topic/112409
但沒有找到答案,請各位幫忙看看.謝謝了~望指教.
===================================================================================
自己測試1:
找一個不可行的方法已經通過了:
把lazyLoadingFilter屏蔽掉可以更新FK,但這個是不能屏蔽的,繼續找方法................
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
但是這個不能屏蔽啦.有哪位知道怎么辦嗎?
===================================================================================
自己測試2:
使用XML配置:
還是存在一樣,看樣子不是這個問題.
===================================================================================
自己測試3:
更新FK時使用非session狀態的BO:
String cargoTypeId = request.getParameter("cargoType.cargoTypeId");
CargoType cargoType = new CargoType();
cargoType.setCargoTypeId(cargoTypeId);
cargo.setCargoType(cargoType);
cargoManager.save(cargo);
此方法在struts下是有效,但在spring mvc下無效.why??
===================================================================================
自己測試4:
執行update之前先clear session:
getSession().clear();//清除緩存
return (T) super.getHibernateTemplate().merge(object);
問題好像已經解決了,但不知道有沒有其它影響.請各位指教.
是不是緩存配置有問題,我把hibernate的緩存設置成false,問題還是照樣存在.
- many to one FK更新問題求解(identifier of an instance)
- identifier of an instance ...... altered from ** to **
- identifier of an instance of
- identifier of an instance of from 1 to 1
- identifier of an instance of was altered from to null
- identifier of an instance of .. altered to null ---- hibernate exception
- identifier of an instance of was altered from to null
- identifier of an instance ... altered from num1 to num2
- identifier of an instance of Object is altered from 2 to 5
- 异常:identifier of an instance of cn.ebank.pojo.News was altered from 2 to 2;
- org.hibernate.HibernateException: identifier of an instance of XXX was altered from X to X
- 关于identifier of an instance of Object was altered from 6 to 6异常解决问题
- org.hibernate.HibernateException: identifier of an instance of XXX was altered from X to X
- merge对比引发的identifier of an instance of entity was altered from .. to ..报错
- identifier of an instance of com.you.hibernate.model.TStudentInfo was altered from 6 to 7
- org.hibernate.HibernateException: identifier of an instance of XXX was altered from X to X
- identifier of an instance was altered from XXXX to XXXX解决
- identifier of an instance of XXX was altered from 75
- linux下IPTABLES配置详解
- 美国人找工作怪招迭出 跟贵人搭讪办就业派对
- 加载图片覆盖整个窗口
- 详细介绍五种JSP跳转方法
- 手把手教你把Vim改装成一个IDE编程环境(图文)
- many to one FK更新問題求解(identifier of an instance)
- 非主流Linux窗口管理器集锦
- 用反射方法使用户控件动态调用父页面的方法
- 平台搭建中出现的小问题
- http 站点 如何配置成 https 站点
- 怎么学习asp.net架构?
- 安装 Ncurses 5.4
- 无处不在的二八原理:在软件开发中的应用
- 当下10大最热门的网站开发技术