2013-05-01 227 views
1

我愿为客户JPA实体,像在Facebook或使用Googe + 加入的朋友有一点困惑JPA实体关系多对多的自我关系

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 + " ]"; 
    } 

} 

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

回答

2

好的做法是同时更新,以保持对象图一致的状态:

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

但联想方面,这是JPA认为只有一面,所有者是其中之一呢不是具有mappedBy属性。所以你可以通过添加一个客户端到clientList来为数据库增加一种新的友谊。

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

+0

感谢您的快速回复 – nkvnkv 2013-05-01 17:32:15