2012-08-07 41 views
2

我正在與JPA的問題。我試圖實現一個數據庫,允許用戶關注其他用戶並被遵循。 我覺得我需要(總結)是這樣的:Twitter喜歡JPA的關係

USER_TABLE: id | userName 
RELATIONSHIP_TABLE: id | follower | followed | acceptation 

我有兩個實體(也總結):

@Entity 
public class User implements Serializable { 

@Id 
private Long id; 

private String userName; 

@OneToMany 
private Collection<Relationship> followings; 

} 


@Entity 
public class Relationship implements Serializable { 

@Id 
private Long id; 

private User follower; 

private User followed; 

private boolean accepted; 

} 

我的問題是,我不知道這是否是可能做到這一點,因爲我獲得了更多的表格,我需要的兩個表格。

任何人都可以幫助我嗎? 感謝和抱歉我的英語。

回答

2

由於您沒有將關聯設爲雙向,您可以獲得更多的表格。 JPA有沒有辦法知道Relationship.followerUser.followings的另一面,如果你不知道:

@Entity 
public class User implements Serializable { 

    @OneToMany(mappedBy = "follower") 
    private Collection<Relationship> followings; 

    // ... 
} 


@Entity 
public class Relationship implements Serializable { 

    @ManyToOne 
    @JoinColumn(name = "follower") 
    private User follower; 

    @ManyToOne 
    @JoinColumn(name = "followed") 
    private User followed; 

    // ... 
} 

當然The documentation解釋說,是如何工作的。

+0

現在我明白了。非常感謝你。 – 2012-08-26 21:51:09