1

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

在此处输入图像描述

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,当我希望将客户添加到我的朋友列表时,不知道该使用哪一个。感谢您的任何建议。

4

1 回答 1

2

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

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

但是JPA考虑的唯一一方是关联的所有者,是没有mappedBy属性的一方。因此,您只需将客户端添加到clientList.

为了更好的可读性,我会为列表选择更好的名称。像clientsWhoAreFriendsOfMe, 和clientsWhoChoseMeAsFriend

于 2013-05-01T16:54:35.340 回答