Hibernate用户和朋友关系添加其他列

时间:2013-12-18 16:43:31

标签: java sql database hibernate

我想通过hibernate建立一个朋友系统,我需要检查用户朋友与该用户相关的相应时间。

我希望表格列如下代码:

table 1:
   int id;
   String name;

table 2:
   int user_id;
   int friend_id;
   Date startdate;   

POJO代码:

public class User {
    private int id;
    private String name;
    private Map<User,Date> friends;
}

1 个答案:

答案 0 :(得分:0)

我认为你只需要JPA。像这样:

@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "friendship", joinColumns = @JoinColumn(name = "user_id"))
    @MapKeyColumn(name = "friend_id")
    @Column(name = "start_date")
    private Map<Long, Date> friendshipMap;

    ... getters/setters ...

}