title: ManyToOne annotation to specific column
body: <p>I am trying to create the follow structure using Hibernate with annotations:</p>
  
  <p><a href="https://i.stack.imgur.com/Ac1Gl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ac1Gl.png" alt="Schema"></a></p>
  
  <p>To create a ManyToMany relationship I had created two classes called SessionRiu and SessionRiuId (pk). This way the Session class has two foreign keys (agend_id and user_id) but when I try to create the OneToMany from operation the hibernate does not create the foreign using the id from session:</p>
  
  <p>Session FKs:</p>
  
  <p><a href="https://i.stack.imgur.com/SDhDl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SDhDl.png" alt="enter image description here"></a></p>
  
  <p>Operation FKs (<strong>empty</strong>):
  <a href="https://i.stack.imgur.com/Nmrxi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nmrxi.png" alt="enter image description here"></a></p>
  
  <p>As I am beginner in java and hibernate, I would appreciate any suggest about my code.</p>
  
  <p>My goal is to create correctly the relationship of these four tables and finally create the fk of Operation class using jpa with the column Operation.session_id reference Session.id.</p>
  
  <p>Exactly that:
  ALTER TABLE Operation ADD FOREIGN KEY (session_id) REFERENCES Sessions(id) </p>
  
  <p>My files:</p>
  
  <p><strong>Base Class:</strong></p>
  
  <pre><code>    /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.riuapp.model;
  
  
  import java.util.Calendar;
  import java.util.Date;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.EnumType;
  import javax.persistence.Enumerated;
  import javax.persistence.GeneratedValue;
  import static javax.persistence.GenerationType.IDENTITY;
  import javax.persistence.Id;
  import javax.persistence.MappedSuperclass;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  
  /**
   *
   * @author vdasilva
   */
  @MappedSuperclass
  public abstract class Base {
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "id", unique = true, nullable = false)
      Long id;
  
      @Column(name="status")
      private StatusEnum status;
  
      @Column(name = "dt_create", columnDefinition="DATETIME")
      @Temporal(TemporalType.TIMESTAMP)
      private Date dt_create;
  
      @Column(name = "dt_update", columnDefinition="DATETIME")
      @Temporal(TemporalType.TIMESTAMP)
      private Date dt_update;
  
      public Base(){
          this.dt_create = new Date();
      }
  
      ///*************************************************************************
      ///GETTERS AND SETTERS    
      ///*************************************************************************
      public Long getId() {
          return id;
      } 
      public void setId(Long id) {
          this.id = id;
      }
  
      @Enumerated(EnumType.ORDINAL)
      public StatusEnum getStatus() {
              return this.status;
      }
  
      public void setStatus(StatusEnum status) {
              this.status = status;
      }
  
      /**
       * @return the dt_create
       */
      public Date getDt_create() {
          return dt_create;
      }
  
      /**
       * @param dt_create the dt_create to set
       */
      public void setDt_create(Date dt_create) {
          this.dt_create = dt_create;
      }
  
      /**
       * @return the dt_update
       */
      public Date getDt_update() {
          return dt_update;
      }
  
      /**
       * @param dt_update the dt_update to set
       */
      public void setDt_update(Date dt_update) {
          this.dt_update = dt_update;
      }  
  
  }
  </code></pre>
  
  <p><strong>Agent class:</strong></p>
  
  <pre><code>    package com.riuapp.model;
  
  
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="Agents", catalog = "TestMvn")
  public class Agent extends Base implements java.io.Serializable {
  
      /*@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      @SuppressWarnings("FieldMayBeFinal")
      private List&lt;SessionRiu&gt; sessions;*/
  
      HashSet&lt;SessionRiu&gt; sessions = new HashSet&lt;SessionRiu&gt;(0);
  
      @Column(name="name")
      String name;
  
      @Column(name="login")
      String login;
  
      @Column(name="pass")
      String pass;
  
      @Column(name="description")
      String description;
  
      @Column(name="client_id")
      Long client_id;
  
  
      public Agent(Long id, String name, String description) {
          //this.sessions = new ArrayList&lt;SessionRiu&gt;();
          this.id = id;
          this.name = name;
          this.description = description;
      }
  
      public Agent() {
          //this.sessions = new ArrayList&lt;SessionRiu&gt;();
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
       public Long getClientId() {
          return client_id;
      }
  
      public void setClientId(Long x) {
          this.client_id = x;
      }    
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.agent", cascade=CascadeType.ALL)
      public Set&lt;SessionRiu&gt; getSessions() {
              return this.sessions;
      }
  
      public void setSessions(HashSet&lt;SessionRiu&gt; sessions) {
              this.sessions = sessions;
      }
  }
  </code></pre>
  
  <p><strong>User class:</strong></p>
  
  <pre><code>package com.riuapp.model;
  
  import java.util.HashSet;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  
  @Entity
  @Table(name="Users", catalog = "TestMvn")
  public class User extends Base implements java.io.Serializable {
  
      private String name;
      private String description;
      private Long client_id;
      private HashSet&lt;SessionRiu&gt; sessions = new HashSet&lt;SessionRiu&gt;(0);
  
      public User(Long id, String name, String description) {
         // this.sessions = new ArrayList&lt;SessionRiu&gt;();
          this.id = id;
          this.name = name;
          this.description = description;
      }
  
      public User() {
          //this.sessions = new ArrayList&lt;SessionRiu&gt;();
      }
  
      @Column(name="name")
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name="description")
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      @Column(name="client_id")
      public Long getClientId() {
          return client_id;
      }
  
      public void setClientId(Long x) {
          this.client_id = x;
      }  
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade=CascadeType.ALL)
      public Set&lt;SessionRiu&gt; getSessions() {
              return this.sessions;
      }
  
      public void setSessions(HashSet&lt;SessionRiu&gt; sessions) {
              this.sessions = sessions;
      }
  }
  </code></pre>
  
  <p><strong>SessionRiuId class:</strong></p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.riuapp.model;
  
  import javax.persistence.Embeddable;
  import javax.persistence.ManyToOne;
  
  @Embeddable
  public class SessionRiuId implements java.io.Serializable {
  
      private Agent agent;
          private User user;
  
      @ManyToOne
      public Agent getAgent() {
          return agent;
      }
  
      public void setAgent(Agent agent) {
          this.agent = agent;
      }
  
      @ManyToOne
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
  
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          SessionRiuId that = (SessionRiuId) o;
  
          if (agent != null ? !agent.equals(that.agent) : that.agent != null) return false;
          if (user != null ? !user.equals(that.user) : that.user != null)
              return false;
  
          return true;
      }
  
      public int hashCode() {
          int result;
          result = (agent != null ? agent.hashCode() : 0);
          result = 31 * result + (user != null ? user.hashCode() : 0);
          return result;
      }
  
  }
  </code></pre>
  
  <p><strong>Session Class:</strong></p>
  
  <pre><code>package com.riuapp.model;
  
  import java.util.HashSet;
  import java.util.Set;
  import javax.persistence.AssociationOverride;
  import javax.persistence.AssociationOverrides;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.EmbeddedId;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.persistence.Transient;
  
  @Entity
  @Table(name = "Sessions", catalog = "TestMvn")
  @AssociationOverrides({
          @AssociationOverride(name = "pk.agent", 
              joinColumns = @JoinColumn(name = "agent_id")),
          @AssociationOverride(name = "pk.user", 
              joinColumns = @JoinColumn(name = "user_id")) })
  public class SessionRiu extends Base implements java.io.Serializable{
  
      private SessionRiuId pk = new SessionRiuId();
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "sessionriu", cascade=CascadeType.ALL, orphanRemoval = true)
      private HashSet&lt;Operation&gt; operations = new HashSet&lt;Operation&gt;(0);
  
      @EmbeddedId
      public SessionRiuId getPk() {
              return pk;
      }
  
      public void setPk(SessionRiuId pk) {
              this.pk = pk;
      }
  
      @Transient
      public Agent getAgent() {
              return getPk().getAgent();
      }
  
      public void setAgent(Agent agent) {
              this.getPk().setAgent(agent);
      }
  
      @Transient
      public User getUser() {
              return this.getPk().getUser();
      }
  
      public void setUser(User user) {
              this.getPk().setUser(user);
      }
  
      private StateEnum state;
  
      @Column(name="duration")
      private double duration;
  
      @Column(name="ip")
      private String ip;
  
      @Column(name="browser")
      private String browser;
  
      public SessionRiu(Agent a, User u) {
          this.state = StateEnum.STATE0;
          this.getPk().setAgent(a);
          this.getPk().setUser(u);
      }
  
      public SessionRiu(StateEnum x, Agent a, User u) {
          this.state = x;
          this.getPk().setAgent(a);
          this.getPk().setUser(u);
      }
  
      /**
       * @return the state
       */
      public StateEnum getState() {
          return state;
      }
  
      /**
       * @param state the state to set
       */
      public void setState(StateEnum state) {
          this.state = state;
      }
  
      /**
       * @return the duration
       */
      public double getDuration() {
          return duration;
      }
  
      /**
       * @param duration the duration to set
       */
      public void setDuration(double duration) {
          this.duration = duration;
      }
  
      /**
       * @return the ip
       */
      public String getIp() {
          return ip;
      }
  
      /**
       * @param ip the ip to set
       */
      public void setIp(String ip) {
          this.ip = ip;
      }   
  
      /**
       * @return the browser
       */
      public String getBrowser() {
          return browser;
      }
  
      /**
       * @param browser the browser to set
       */
      public void setBrowser(String browser) {
          this.browser = browser;
      }
  
  
      public HashSet&lt;Operation&gt; getOperations() {
              return this.operations;
      }
  
      public void setOperations(HashSet&lt;Operation&gt; operations) {
              this.operations = operations;
      }   
  
  }
  </code></pre>
  
  <p>And finally the <strong>Operation class</strong>:</p>
  
  <pre><code>package com.riuapp.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Operation", catalog = "TestMvn")
  public class Operation extends Base implements java.io.Serializable{
  
      @ManyToOne
      @JoinColumn(referencedColumnName = "id")
      private SessionRiu sessionRiu;
  
      private StateEnum state;
      private ResultEnum result;
  
      @Column(name="duration")
      private double duration;
  
      @Column(name="ip")
      private String ip;
  
      @Column(name="browser")
      private String browser;    
  
      public Operation(SessionRiu sessionRiu) {
          this.state = StateEnum.STATE0;
          this.result = ResultEnum.PENDING;
          this.sessionRiu = sessionRiu;
      }
  
      public Operation(SessionRiu sessionRiu, StateEnum state, ResultEnum result) {
          this.state = state;
          this.result = result;
          this.sessionRiu = sessionRiu;
      }
  
      /**
       * @return the state
       */
      public StateEnum getState() {
          return state;
      }
  
      /**
       * @param state the state to set
       */
      public void setState(StateEnum state) {
          this.state = state;
      }
  
      /**
       * @return the duration
       */
      public double getDuration() {
          return duration;
      }
  
      /**
       * @param duration the duration to set
       */
      public void setDuration(double duration) {
          this.duration = duration;
      }
  
      /**
       * @return the ip
       */
      public String getIp() {
          return ip;
      }
  
      /**
       * @param ip the ip to set
       */
      public void setIp(String ip) {
          this.ip = ip;
      }   
  
      /**
       * @return the browser
       */
      public String getBrowser() {
          return browser;
      }
  
      /**
       * @param browser the browser to set
       */
      public void setBrowser(String browser) {
          this.browser = browser;
      }
  
      public SessionRiu getSessionRiu() 
      {
              return this.sessionRiu;
      }
  
      public void setSessionRiu(SessionRiu sessionRiu) 
      {
              this.sessionRiu = sessionRiu;
      }
  }
  </code></pre>
  
  <p>Observation:</p>
  
  <p>if I remove the join line are created two fks (agent_id and user_id) as the session table =(</p>
  
  <pre><code>@Entity
  @Table(name = "Operation", catalog = "TestMvn")
  public class Operation extends Base implements java.io.Serializable{
  
      @ManyToOne
      //@JoinColumn(referencedColumnName = "id")
      private SessionRiu sessionRiu;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TGbsd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TGbsd.png" alt="enter image description here"></a></p>