2013-05-01 231 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