我有两个实体,Document
和BodyElement
,并试图用 Hibernate 4.2 持久化它们。mtdt_t
正确填充,但表中的外docid
键mtdt_body_t
是NULL
.
我看到休眠试图在没有docid
值的情况下插入。insert into mtdt_body_t values ( )
@Entity
@Table(name = "mtdt_t")
public class Document implements Serializable {
@Id
@Column(name = "docid", unique = true, nullable = false)
private String docid;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@OrderColumn
@JoinColumn(name = "docid", nullable = false)
private BodyElement bodyElement;
public String getDocid() {
return docid;
}
public void setDocid(String docid) {
this.docid = docid;
}
public BodyElement getBodyElement() {
return bodyElement;
}
public void setBodyElement(BodyElement bodyElement) {
this.bodyElement = bodyElement;
}
}
@Entity
@Table(name = "mtdt_body_t")
public class BodyElement implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
@JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)
private Document document;
public BodyElement() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
}
我离开了另一个领域。在Document
我有,
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@OrderColumn
@JoinColumn(name = "docid", nullable = false)
@XmlPath("head/meta/kb:keywords/kb:keyword")
private Set<Keyword> keywords;
在Keyword
课堂上,我将外键映射为,
@ManyToOne
@JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)
@XmlTransient
private Document document;
而那个docid
领域永远不会NULL
。
@OneToOne
与Mapping 相比,有什么特别之处@OneToMany
吗?@OneToMany
我只是模仿了我在球场上所做的事情@OneToOne
。
谢谢