2012-08-07 64 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