wsa*_*ams 5 hibernate one-to-one
我有两个实体,Document
并BodyElement
试图用Hibernate 4.2来坚持它们.mtdt_t
填入正确的,但外键docid
的mtdt_body_t
表NULL
.
我看到hibernate试图插入没有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;
}
}
Run Code Online (Sandbox Code Playgroud)
我离开了另一个领域.在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;
Run Code Online (Sandbox Code Playgroud)
在Keyword
类中我将外键映射为,
@ManyToOne
@JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)
@XmlTransient
private Document document;
Run Code Online (Sandbox Code Playgroud)
那个docid
领域永远不会NULL
.
@OneToOne
相比之下,Mapping 有什么特别之处@OneToMany
吗?我只是模仿我@OneToMany
在@OneToOne
球场上所做的一切.
谢谢
小智 -1
OrderColumn 注释在 OneToMany 或 ManyToMany 关系或元素集合上指定。OrderColumn 注释在引用要排序的集合的关系一侧指定。顺序列作为实体或可嵌入类的状态的一部分不可见。\n文档链接
您在映射时出错。您忘记了mappedBy 属性。这意味着实体之间的关系已经被映射,所以你不会这样做两次。您只需使用mappedBy 属性(下面是这篇文章)说“嘿,它在那里完成了” 。这是有用的示例:Hibernate \xe2\x80\x93 一对一示例(注释)
归档时间: |
|
查看次数: |
7078 次 |
最近记录: |