我有兩個實體Document
和BodyElement
,並試圖用Hibernate 4.2持久化它們。 mtdt_t
正確填充,但mtdt_body_t
表中的外鍵docid
爲NULL
。@OneToOne與休眠/ JPA映射的外鍵字段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
相比有什麼特別之處嗎?我只是模仿我在@OneToOne
字段中爲@OneToMany
所做的工作。
由於
我跟着mkyong的例子使用'mappedBy',但它仍然離開'docid'字段。這一次'docid'專欄甚至不存在。 – wsams