Note that there are some explanatory texts on larger screens.

plurals
  1. POjpa entity relation many to many self relation
    text
    copied!<p>I wish to make jpa entity for clients, something like adding friends on facebook or googe+ Got a bit confused </p> <p><img src="https://i.stack.imgur.com/aTaG9.png" alt="enter image description here"></p> <pre><code>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&lt;Client&gt; clientList; @ManyToMany(mappedBy = "clientList") private List&lt;Client&gt; 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&lt;Client&gt; getClientList() { return clientList; } public void setClientList(List&lt;Client&gt; clientList) { this.clientList = clientList; } @XmlTransient public List&lt;Client&gt; getClientList1() { return clientList1; } public void setClientList1(List&lt;Client&gt; 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 &amp;&amp; other.clientId != null) || (this.clientId != null &amp;&amp; !this.clientId.equals(other.clientId))) { return false; } return true; } @Override public String toString() { return "entities.Client[ clientId=" + clientId + " ]"; } } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload