Hibernate映射问题

时间:2013-11-19 03:11:27

标签: java hibernate

我有班级用户和朋友

users.java


package schema;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import edu.usc.bg.base.ByteIterator;
import edu.usc.bg.base.ObjectByteIterator;

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

        @Id
        private Integer userid=-1;

        private String username;
        private String pw;
        private String fname;
        private String lname;
        private String gender;
        private String dob;
        private String jdate;
        private String ldate;
        private String address;
        private String email;
        private String tel;



        @Column(name="pic")
        @Lob
        private byte[] pic;

        @Column(name="tpic")
        @Lob
        private byte[] tpic;




        //if only entry in friend1 means pending request
        @OneToMany(mappedBy="user1",orphanRemoval=true,cascade=CascadeType.ALL)
        private Set<friendship>pendingFriendList=new HashSet<>();


        //if entry for user in friend2 too then confirm friends
        @OneToMany(mappedBy="user2",orphanRemoval=true,cascade=CascadeType.ALL)
        private Set<friendship>confirmedFriendList=new HashSet<>();

}

这里,在友谊课中,我有两列,每列都有两个用户对象参考

friendhsip.java

package schema;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@IdClass(Pk.class)
@Table(name="friendship")
public class friendship implements Serializable {


    @Id
    @ManyToOne
    @JoinColumn(name="inviterid",referencedColumnName="userid")
    public users user1;

    @Id
    @ManyToOne
    @JoinColumn(name="inviteeid",referencedColumnName="userid")
    public users user2;

    private int status=1;


    public users getUser1() {
        return user1;
    }
    public void setUser1(users user1) {
        this.user1 = user1;
    }
    public users getUser2() {
        return user2;
    }
    public void setUser2(users user2) {
        this.user2 = user2;
    }


    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }


}

但问题是当我为特定用户对象添加一个友谊对象说“PendingFriendList”时,同样的东西被添加到该对象的“ConfirmedFriendList”中。我怎么能避免这种情况?

1 个答案:

答案 0 :(得分:0)

是的,这是有道理的。你有一个朋友到朋友的@OneToMany两次 - 等待和确认 - 但是没有机制将待处理列表中的元素限制为相关条目,并且相同的条目也被确认,这就是为什么你在两者中都获得相同的记录

有各种解决方案。

两个你可以看看:

一,在每个集合上使用Hibernate(非JPA投诉)@Where注释

@OneToMany(mappedBy="user2",orphanRemoval=true,cascade=CascadeType.ALL)
@Where("where status = xyz")
private Set<friendship>confirmedFriendList=new HashSet<>();

两个,对每个集合使用具有不同连接表的连接表:

@OneToMany(mappedBy="user2",orphanRemoval=true,cascade=CascadeType.ALL)
@JoinTable("....")
private Set<friendship>confirmedFriendList=new HashSet<>();

P.S。在java类名中,始终以大写字母开头。