Hibernate级联更新问题
我有两个表Department部门表和Users用户表,两个表之间是一对多的关系,Users表外键关联Department表的部门编码(code)字段。两个表的domain如下,现在我想在更新Department表的编码的时候,级联更新部门下属的所有用户的外键引用,请问该怎么做?
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_Bmzl")
public class Department implements Serializable {
/**
*
*/
private static final long serialVersionUID = -351783446768387127L;
public Department(){
}
public Department(String code){
this.code = code;
}
public Department(Integer id, String code, String name, String parentID, String parentDepartName,Double jd,Double wd){
super();
this.id = id;
this.code = code;
this.name = name;
this.parentID = parentID;
this.parentDepartName = parentDepartName;
this.lat = wd;
this.lon = jd;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "BmID", unique = true, nullable = false, length = 20)
private String code;
@Column(name = "BmName", nullable = false, length = 255)
private String name;
@Column(nullable = false, length = 10)
private String type;
@Column(name = "lon")
private Double lon;
@Column(name = "lat")
private Double lat;
public Double getLon() {
return lon;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public void setLon(Double lon) {
this.lon = lon;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getParentID() {
return parentID;
}
public void setParentID(String parentID) {
this.parentID = parentID;
}
@Column(name = "SjbmID", length = 20)
private String parentID;
@Column(name = "SjbmName", length = 255)
private String parentDepartName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentDepartName() {
return parentDepartName;
}
public void setParentDepartName(String parentDepartName) {
this.parentDepartName = parentDepartName;
}
}
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_Users")
public class Users {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer userID;
@Column(name="UserName",unique=true)
private String userName;
@Column(name="Password")
private String password;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="BmzlID",referencedColumnName="BmID")
private Department dpt;
@Column(name="NickName")
private String nickName;
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Department getDpt() {
return dpt;
}
public void setDpt(Department dpt) {
this.dpt = dpt;
}
}
用户表中我已经配置了cascade = CascadeType.MERGE,但是更新时一直报外键约束异常,期待大哥大牛给出一个解决方案 --------------------编程问答-------------------- 给个错误信息 --------------------编程问答-------------------- 错误信息
--------------------编程问答-------------------- 在线等待!快来人呐 --------------------编程问答-------------------- 只在Hibernate中配置关联关系,不要在数据库中设置Department部门表和Users用户表的主外键关联关系;
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not update: [cn.com.niat.shunyang.domain.Department#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2443)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2325)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2625)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:115)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at cn.com.niat.shunyang.init.Init.main(Init.java:53)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: UPDATE 语句与 REFERENCE 约束"FK9F52DA5D8EBD4B05"冲突。该冲突发生于数据库"shunyang",表"dbo.t_Users", column 'BmzlID'。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.getPrepExecResponse(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PreparedStatementExecutionRequest.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.CancelableRequest.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeRequest(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2421)
... 12 more
或者部门表中增加无意义的ID作为主键,用ID关联Users表的主键,Department表的编码不作为主键
补充:Java , Web 开发