2014-02-13 73 views
0

我有2個表。第二個表包含兩個FK到第一個(其含義是用戶可以有一組其他用戶)。如何在Hibernate中註釋具有相同類型實體的實體

CREATE TABLE "user" (
       usr_id INTEGER PRIMARY KEY, 
       usr_login VARCHAR NOT NULL UNIQUE, 
); 

CREATE TABLE user_team (
     utm_id INTEGER PRIMARY KEY, 
     usr_id INTEGER NOT NULL UNIQUE, 
     manager_id INTEGER NOT NULL, 
     CONSTRAINT utm_usr_fk FOREIGN KEY (usr_id) 
       REFERENCES "user" (usr_id) ON DELETE CASCADE, 
     CONSTRAINT utm_manager_fk FOREIGN KEY (manager_id) 
       REFERENCES "user" (usr_id) ON DELETE CASCADE 
); 

我可以在User類中編寫一些註釋映射以擁有一組用戶(團隊)嗎? 例如:

@Entity 
@Table(name = "user") 
public class User implements Serializable { 
    @OneToMany ?????????????????????? 
    private List<User> team; 
} 
+1

可能的重複 - http://stackoverflow.com/a/3393662/738746 –

+0

據我所知在這個例子中有一個表。但我有兩張桌子。 – irko

回答

1
@JoinTable(name = "user_team", joinColumns = { @JoinColumn(name = "usr_id") }, inverseJoinColumns = { @JoinColumn(name = "manager_id") }) 

user_team將不utm_id列。這很關鍵嗎?

+0

不幸的是,它不適合我。 – irko

+0

我試圖改變usr_id和manager_id的位置。之後,它一次工作,但現在列表是空的。我不知道是什麼問題。 @JoinTable(name =「user_team」,joinColumns = {@JoinColumn(name =「usr_id」)},inverseJoinColumns = {@JoinColumn(name =「manager_id」)}) – irko

+0

這對我很有用:@JoinTable(name = user_team「,joinColumns = {@JoinColumn(name =」manager_id「)},inverseJoinColumns = {@JoinColumn(name =」usr_id「)}) – irko

相關問題