jpa entity relation many to many self relation

I wish to make jpa entity for clients, something like adding friends on facebook or googe+ Got a bit confused

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

}

I have clientList and clientList1, do not know which one to use when i wish to add a client to my list of friends. Thanks for any advice.

Answers


The good practice is to update both, to keep the object graph in a consistent state:

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

But the owner of the association, which is the only side that JPA considers, is the one which does not have the mappedBy attribute. So you could add a new friendship to the database by only adding a client to clientList.

For better readability, I would choose better names for the lists. Something like clientsWhoAreFriendsOfMe, and clientsWhoChoseMeAsFriend.


Need Your Help

SSL form POST from one site to another

encryption ssl webforms http-post

I am attempting to do a POST securely from one website to another. Instead of trying to do our own encryption or hashing I would like to use SSL. Is it possible to do a form post from one site to a...

Android - Cannot resize AlertDialog

java android alertdialog

I am trying to resize an AlertDialog, which I have done before, but cannot remember how I did it. Below is the code Ive used, but it just inst working. Is anyone able to point out what I am doing w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.