我有以下三个课程:
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Type;
@Entity
public class EventTO {
...
private List<EventAssignedResourceTO> eventAssignedResourceTOList;
/**
* @return the eventAssignedResourceTOList
*/
@OneToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name = "EAR_EVN_ID")
public List<EventAssignedResourceTO> getEventAssignedResourceTOList() {
return eventAssignedResourceTOList;
}
/**
* @param eventAssignedResourceTOList
* the eventAssignedResourceTOList to set
*/
public void setEventAssignedResourceTOList(List<EventAssignedResourceTO> eventAssignedResourceTOList) {
this.eventAssignedResourceTOList = eventAssignedResourceTOList;
}
public void setPrivate(boolean aPrivate) {
setPrivateEvent(aPrivate);
}
}
第二类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
public class EventAssignedResourceTO {
private EventTO eventTO;
private EventResourceTO eventResourceTO;
private int resourceCount;
/**
* @return the eventTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_EVN_ID", updatable = false, insertable=false, nullable = false)
public EventTO getEventTO() {
return eventTO;
}
/**
* @param eventTO
* the eventTO to set
*/
public void setEventTO(EventTO eventTO) {
this.eventTO = eventTO;
}
/**
* @return the eventResourceId
*/
@Transient
public Long getEventResourceId() {
return eventResourceId;
}
/**
* @param eventResourceId
* the eventResourceId to set
*/
public void setEventResourceId(Long eventResourceId) {
this.eventResourceId = eventResourceId;
}
/**
* @return the resourceCount
*/
@Column(name = "EAR_COUNT")
public int getResourceCount() {
return resourceCount;
}
/**
* @param resourceCount
* the resourceCount to set
*/
public void setResourceCount(int resourceCount) {
this.resourceCount = resourceCount;
}
/**
* @return the eventResourceTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_ERS_ID")
public EventResourceTO getEventResourceTO() {
return eventResourceTO;
}
/**
* @param eventResourceTO
* the eventResourceTO to set
*/
public void setEventResourceTO(EventResourceTO eventResourceTO) {
this.eventResourceTO = eventResourceTO;
}
}
第三类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
@Entity
public class EventResourceTO {
private String name;
private int count;
private boolean active;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
@Column(name = "ERS_ID")
public Long getId() {
return super.getId();
}
/**
* @return the name
*/
@Column(name="ERS_NAME")
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the count
*/
@Column(name="ERS_COUNT")
public int getCount() {
return count;
}
/**
* @param count
* the count to set
*/
public void setCount(int count) {
this.count = count;
}
/**
* @return the active
*/
@Type(type="yes_no")
@Column(name="ERS_IS_ACTIVE")
public boolean isActive() {
return active;
}
/**
* @param active
* the active to set
*/
public void setActive(boolean active) {
this.active = active;
}
}
我在这里遵循了官方休眠文档:http: //docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#collections-bidirectional
这个例子在这里:http: //viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/
问题是,在 DB 中插入 EventTO 时,我在 eventId 上遇到约束违规,因为休眠无法在 EventAssignedResourceTO 的 eventTO 中设置新创建事件的 id 并且 id 为 null 。
所以我决定去掉EventAssignedResourceTO中JoinColumn注解中的可更新和可插入标签。
现在插入工作正常但是当我想删除一个事件时,休眠首先删除 AssignedResourceTO 然后是事件,最后我不知道为什么它会尝试更新其表中的 AssignedResourceTO 记录。在这里,它尝试将 eventTO 的 id 设置为 NULL 并且我得到约束违规。更新也会发生同样的情况。
我真的很困惑,因为官方文档中写的内容不起作用,而且我无法在网上找到任何解决方案。
任何的想法??
干杯