6

我有两个实体,DocumentBodyElement,并试图用 Hibernate 4.2 持久化它们。mtdt_t正确填充,但表中的外docidmtdt_body_tNULL.

我看到休眠试图在没有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

谢谢

4

1 回答 1

-1
  1. OrderColumn 注释在 OneToMany 或 ManyToMany 关系或元素集合上指定。OrderColumn 注释在引用要排序的集合的关系一侧指定。order 列作为实体或可嵌入类的状态的一部分不可见。 文档链接

  2. 你映射有误。您忘记了 mappedBy 属性。这意味着实体之间的关系已经被映射,所以你不要这样做两次。您只需使用 mappedBy 属性说“嘿,它已经完成了”(接下来是这篇文章)。这是一个有用的例子:Hibernate – 一对一的例子(注解)

于 2013-08-19T14:24:21.573 回答