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}),不讓它級聯更新,也可以,但這只是一個暫的辦法

你可能感兴趣的:(java,Hibernate,jsp,Access)