many to one FK更新問題求解(identifier of an instance)

錯誤如下:
Data Access Failure

identifier of an instance of com.tar.model.CargoType was altered from 402881821bdb7471011bdb75c08e0002 to 402881821bdb7471011bdb7586530001; nested exception is org.hibernate.HibernateException: identifier of an instance of com.tar.model.CargoType was altered from 402881821bdb7471011bdb75c08e0002 to 402881821bdb7471011bdb7586530001

我在論壇上看看別以前發的帖子,沒有找到答案,求解呀!
子類:

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;
}
}

父類:

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)

cascade變成:
@ManyToOne(cascade = {CascadeType.PERSIST}),不讓它級聯更新,也可以,但這只是一個暫的辦法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值