Hibernate用户和朋友关系添加其他列 [英] Hibernate user and friend relationship add other column

查看:95
本文介绍了Hibernate用户和朋友关系添加其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过hibernate做一个朋友系统,我需要检查用户朋友的相应时间,当这个用户与其他人有关系。

I want to make a friend system via hibernate, and I need to check user-friend's corresponding time when this user had related to other.

我希望表列如下代码:

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;
}


推荐答案

然后JPA。类似的东西:

I think you need nothing more then JPA. Something like this:

@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 ...

}

这篇关于Hibernate用户和朋友关系添加其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆