开发者

Add row to join table using JPA

I'm using JPA 2.0 and I'm using a generated schema.

Here is my mapping:

@Entity
@Table(name = "CBV_USER")
public class CbvUser implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "Login")
    private String login;

    @Basic(optional = false)
    @Column(name = "Password")
    private String password;

    @Basic(optional = false)
    @Column(name = "Email")
    private String email;

    @Basic(optional = false)
    @Column(name = "FirstName")
    private String firstName;

    @Basic(optional = false)
    @Column(name = "LastName")
    private String lastName;

    @Basic(optional = false)
  开发者_StackOverflow社区  @Column(name = "Id")
    private String id;

    @Column(name = "Score")
    private BigDecimal score;

    @JoinTable(name = "FRIENDSHIP", joinColumns = {
        @JoinColumn(name = "Login0", referencedColumnName = "Login")}, inverseJoinColumns = {
        @JoinColumn(name = "Login1", referencedColumnName = "Login")})
    @ManyToMany
    private List<CbvUser> cbvUserList2;

    @ManyToMany(mappedBy = "cbvUserList2")
    private List<CbvUser> cbvUserList3;

    public CbvUser() {
    }

    public CbvUser(String login) {
        this.login = login;
    }

    public CbvUser(String login, String password, String email, String firstName, String lastName, String id) {
        this.login = login;
        this.password = password;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public BigDecimal getScore() {
        return score;
    }

    public void setScore(BigDecimal score) {
        this.score = score;
    }

    public List<CbvUser> getCbvUserList2() {
        return cbvUserList2;
    }

    public void setCbvUserList2(List<CbvUser> cbvUserList2) {
        this.cbvUserList2 = cbvUserList2;
    }

    public List<CbvUser> getCbvUserList3() {
        return cbvUserList3;
    }

    public void setCbvUserList3(List<CbvUser> cbvUserList3) {
        this.cbvUserList3 = cbvUserList3;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (login != null ? login.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CbvUser)) {
            return false;
        }
        CbvUser other = (CbvUser) object;
        if ((this.login == null && other.login != null) || (this.login != null && !this.login.equals(other.login))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "models.CbvUser[login=" + login + "]";
    }

}

My problem is that I can't figure out how to add a new row in the Join Table FRIENDSHIP through a specific CbvUser or through an EntityManager.

I'll be really grateful for any help.


Something like this should work:

CbvUser user1 = new CbvUser();
...
CbvUser user2 = new CbvUser();
...

// declare user2 as a friend of user1
List<CbvUser> cbvUserList2 = new ArrayList<CbvUser>();
cbvUserList2.add(user2);
user1.setCbvUserList2(cbvUserList2);

// declare user1 as a friend of user2
List<CbvUser> cbvUserList3 = new ArrayList<CbvUser>();
cbvUserList3.add(user1);
user2.setCbvUserList3(cbvUserList3);

em.persist(user1);
em.persist(user2);
em.flush();

The friendship relation is a (self-referencing) bidirectional association, so you must set both sides of the link correctly (from user1 to user2 and from user2 to user1).


Providing a public add/remove that maintains referential integrity might be even more elegant here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜