在同一张桌子上多对多地添加列 [英] Many-to-many on the same table with additional columns

查看:107
本文介绍了在同一张桌子上多对多地添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级用户。用户可以成为许多其他用户的朋友。这种关系是相互的。如果A是B的朋友,那么B是A的朋友。另外,我希望每个关系都存储其他数据 - 例如两个用户成为朋友的日期。所以这是在同一张桌子上的多对多关系,并增加了一些列。我知道应该创建一个中产阶级友谊(包含两个用户ID和日期栏)。但是我将这个映射到Hibernate中来。阻止我的是映射到同一个表。我可以解决它,如果多对多关系是在两个不同的表之间。


多对多关系在同一张桌子上

这不是一个好主意。这是一个噩梦维持。



尝试这一个,而不是

  @Entity 
public class Friend {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer friendId;

@Column
私人字符串名称;

@OneToMany(mappedBy =我)
私人列表< MyFriends>我的朋友;

}

@Entity
public class MyFriends {

@EmbeddedId
MyFriendsId id;

@Column
private String additionalColumn;

@ManyToOne
@JoinColumn(name =ME_ID,insertable = false,updateable = false)
private Friend me;

@ManyToOne
@JoinColumn(name =MY_FRIEND_ID,可插入= false,可更新= false)
私人朋友myFriend;

@Embeddable
public static class MyFriendsId implements Serializable {

@Column(name =ME_ID,nullable = false,updateable = false)
私人整数meId;

@Column(name =MY_FRIEND_ID,nullable = false,updateable = false)
private Integer myFriendId;
$ b $ public boolean equals(Object o){
if(o == null)
return false;

if(!(o instanceof MyFriendsId))
return false;

MyFriendsId other =(MyFriendsId)o;如果(!(other.getMeId()。equals(getMeId()))
返回false;

if(!(other.getMyFriendId()。equals(getMyFriendId )))
返回false;

返回true;
}

public int hashcode(){
// hashcode impl




$ b}

问候,

I have a class User. A user can be a friend with many other users. The relationship is mutual. If A is a friend of B then B is a friend of A. Also I want every relation to store additional data - for example the date when two users became friends. So this is a many-to-many relationship on the same table with additional columns. I know that a middle class Friendship should be created(containing two user ids and column for the date). But I am coming short at mapping this with Hibernate. The thing that stops me is that the mapping is to the same table. I can solve it, if the many-to-many relationship was between two different tables.

解决方案

You have said

many-to-many relationship on the same table

It is not a good idea. It is a nightmare to maintain.

Try this one instead

@Entity
public class Friend {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer friendId;

    @Column
    private String name;

    @OneToMany(mappedBy="me")
    private List<MyFriends> myFriends;

}

@Entity
public class MyFriends {

    @EmbeddedId
    private MyFriendsId id;

    @Column
    private String additionalColumn;

    @ManyToOne
    @JoinColumn(name="ME_ID", insertable=false, updateable=false)
    private Friend me;

    @ManyToOne
    @JoinColumn(name="MY_FRIEND_ID", insertable=false, updateable=false)
    private Friend myFriend;

    @Embeddable
    public static class MyFriendsId implements Serializable {

        @Column(name="ME_ID", nullable=false, updateable=false)
        private Integer meId;

        @Column(name="MY_FRIEND_ID", nullable=false, updateable=false)
        private Integer myFriendId;

        public boolean equals(Object o) {
            if(o == null)
                return false;

            if(!(o instanceof MyFriendsId))
                return false;

            MyFriendsId other = (MyFriendsId) o;
            if(!(other.getMeId().equals(getMeId()))
                return false;

            if(!(other.getMyFriendId().equals(getMyFriendId()))
                return false;

            return true;
        }

        public int hashcode() {
            // hashcode impl
        }

    }


}

regards,

这篇关于在同一张桌子上多对多地添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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