我有以下三个课程:
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;
}
}
我已经按照官方的hibernate文档进行了操作: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上遇到约束违规,因为hibernate无法在EventAssignedResourceTO中的eventTO中设置新创建的事件的id,并且id为null。
所以我决定删除EventAssignedResourceTO中JoinColumn注释中的可更新和可插入标记。
现在插件工作正常,但是当我想删除一个事件时,hibernate首先删除AssignedResourceTO然后事件,最后我不知道为什么它会尝试在其表中更新AssignedResourceTO的记录。在这里它尝试将eventTO的id设置为NULL并且我得到约束违规。更新也是如此。
我真的很困惑,因为官方文档中的内容不起作用,我无法在网上找到任何解决方案。
任何想法?
干杯
答案 0 :(得分:2)
@JoinColumn(name = "EAR_EVN_ID")
课程中不需要EventTO
。而是将mappedBy
添加到关联。所以:
@OneToMany(mappedBy = "eventTO", fetch = FetchType.LAZY,cascade=CascadeType.ALL)
public List<EventAssignedResourceTO> getEventAssignedResourceTOList() {
return eventAssignedResourceTOList;
}
对于fetch
关联,LAZY
默认为@OneToMany
,您也可以将其删除。
答案 1 :(得分:0)
对于双向映射,仅在父级使用JoinColumn批注,并在子级中使用mappingBy属性。要删除父级和子级实体之间的重复出现,请使用这些批注。
@OneToMany(
mappedBy = "queue_group",fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
@JsonManagedReference
private Set<Queue> queues;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "qid")
// @JsonIgnore
@JsonBackReference
private Queue_group queue_group;