jpa实体关系多对多的自我关系

时间:2013-05-01 16:43:24

标签: database jpa many-to-many

我希望为客户制作jpa实体,例如在facebook或googe +上添加朋友 有点困惑

enter image description here

This auto generates to code below,
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author nikola
 */
@Entity
@Table(name = "Client")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
    @NamedQuery(name = "Client.findByClientId", query = "SELECT c FROM Client c WHERE c.clientId = :clientId"),
    @NamedQuery(name = "Client.findByClientName", query = "SELECT c FROM Client c WHERE c.clientName = :clientName")})
public class Client implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ClientId")
    private Integer clientId;
    @Size(max = 45)
    @Column(name = "ClientName")
    private String clientName;
    @JoinTable(name = "Friends", joinColumns = {
        @JoinColumn(name = "Client_ClientId", referencedColumnName = "ClientId")}, inverseJoinColumns = {
        @JoinColumn(name = "Client_ClientId1", referencedColumnName = "ClientId")})
    @ManyToMany
    private List<Client> clientList;
    @ManyToMany(mappedBy = "clientList")
    private List<Client> clientList1;

    public Client() {
    }

    public Client(Integer clientId) {
        this.clientId = clientId;
    }

    public Integer getClientId() {
        return clientId;
    }

    public void setClientId(Integer clientId) {
        this.clientId = clientId;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    @XmlTransient
    public List<Client> getClientList() {
        return clientList;
    }

    public void setClientList(List<Client> clientList) {
        this.clientList = clientList;
    }

    @XmlTransient
    public List<Client> getClientList1() {
        return clientList1;
    }

    public void setClientList1(List<Client> clientList1) {
        this.clientList1 = clientList1;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (clientId != null ? clientId.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 Client)) {
            return false;
        }
        Client other = (Client) object;
        if ((this.clientId == null && other.clientId != null) || (this.clientId != null && !this.clientId.equals(other.clientId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Client[ clientId=" + clientId + " ]";
    }

}

我有clientList和clientList1,当我想将客户端添加到我的朋友列表时,不知道要使用哪一个。 谢谢你的任何建议。

1 个答案:

答案 0 :(得分:2)

好的做法是更新两者,以使对象图保持一致状态:

Client mainClient = ...;
Client newFriend = ...;
mainClient.getClientList().add(newFriend);
newFriend.getClientList1().add(mainClient);

但该协会的所有者,即JPA认为唯一的一方,是具有mappedBy属性的所有者。因此,您只需将客户端添加到clientList即可向数据库添加新的友谊。

为了更好的可读性,我会为列表选择更好的名称。类似于clientsWhoAreFriendsOfMeclientsWhoChoseMeAsFriend