2014-11-23 140 views
0

我有以下映射:休眠。 OneToOne映射數據庫

@Entity 
@Table(name="terminal_user") 
public class TerminalUser { 

    @OneToMany(mappedBy="user",fetch = FetchType.LAZY) 
    private Set<UserContent> userContentSet; 
    .... 
} 

@Entity 
@Table(name="user_content") 
public class UserContent { 

    @ManyToOne 
    @JoinColumn(name = "user_id") 
    TerminalUser user; 

    @OneToOne(mappedBy = "userContent") 
    Content content; 
    ... 
} 

@Entity 
@Table(name = "content") 
public class Content { 
@OneToOne 
    @JoinColumn(name = "user_content_id") 
    UserContent userContent; 
    .... 
} 

設置hibernate.hbm2ddl.auto後創建

我看到下面的數據庫圖表:

enter image description here

對我來說關係表user_contentcontent不是一一對應的。

它看起來一樣的terminal_useruser_content關係,它是一對多

做什麼,我錯了嗎?

+0

我知道這並不能回答你的問題,但你爲什麼不只是使用逆向工程工具?它讓生活變得更加輕鬆!讓你的數據庫準備好,然後「魔術」! – 2014-11-23 13:20:08

+0

例如sybase電源設計師? – gstackoverflow 2014-11-23 13:34:18

+0

只需要從數據庫到代碼的hibernate工具。你可以使用MySQL工作臺或任何你想使用的工具,從圖表到數據庫。這就是我一直這樣做的。我從來沒有寫我自己的映射。我只是添加什麼hibernate不會自動生成。 – 2014-11-23 20:22:53

回答

0

一個獨特的約束會使得不同:

@Entity 
@Table(name = "content") 
public class Content { 
@OneToOne 
    @JoinColumn(name = "user_content_id", unique=true) 
    UserContent userContent; 
    .... 
}