title: Spring Boot JPA - Join by two columns
body: <p><strong>TL;DR : Help me join a table by two columns, one by fixed and another by variable value. It is @OneToOne.</strong></p>
  
  <p><strong><em>Do this but in hibernate magic :</em></strong></p>
  
  <pre><code>SELECT t.*, c.*
  FROM grid.app_users t
    JOIN app_dictionaries c on c.primary = 'USER_STATES' and c.secondary = t.state
  ;
  </code></pre>
  
  <hr/>
  
  <p>I was trying to add another join column to my user entity (mapped to table <code>app_users</code>).</p>
  
  <p><a href="https://i.stack.imgur.com/5z695.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5z695.jpg" alt="enter image description here"></a></p>
  
  <p>This column has to be filtered by <code>primary</code> (fixed <em>USER_STATES</em>) and <code>secondary</code>   (variable, in this case <em>A</em> or <em>P</em>) codes on <code>app_dictionaries</code> table.</p>
  
  <p><a href="https://i.stack.imgur.com/qwDXU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qwDXU.jpg" alt="enter image description here"></a></p>
  
  <p>Help me find a suitable join approach.</p>
  
  <p>I could have just created a <strong><em>view</em></strong> and joined it by the above mentioned columns but that entails losing the ability to <code>insert</code> and <code>update</code> on <code>app_users</code>. </p>
  
  <p>I am not very friendly with Hibernate and Spring JPA Repository.</p>
  
  <p><strong>My User entity :</strong></p>
  
  <pre><code>package com.core.app.admin.entity;
  
  import com.core.app.admin.auth.ValidPassword;
  import com.core.app.admin.repository.RState;
  import com.core.app.dicts.entity.EDict;
  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  import lombok.Data;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import javax.persistence.*;
  import java.util.Set;
  
  @Entity
  @Table(name = "app_users")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  @Data
  public class EUser {
  
    @Autowired
    RState repository;
  
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int userId;
    @Column(name = "user_group_id")
    private int userGroupId;
    @Column(name = "name")
    private String name;
    @Column(name = "login")
    private String login;
    @ValidPassword
    @Column(name = "password")
    private String password;
    @Column(name = "state")
    private String state;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(
      name = "app_user_roles",
      joinColumns = @JoinColumn(name = "user_id"),
      inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set&lt;ERole&gt; roles;
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "app_dictionaries")
    private EDict stateName = repository.getByPrimaryAndSecondary();
  
  }
  </code></pre>
  
  <p><strong>My dictionary entity :</strong></p>
  
  <pre><code>package com.core.app.dicts.entity;
  
  import lombok.Data;
  
  import javax.persistence.*;
  import java.io.Serializable;
  
  @Entity
  @Table(
    name = "app_dictionaries",
    uniqueConstraints = {@UniqueConstraint(columnNames = {"primary", "secondary"})}
  )
  @Data
  public class EDict implements Serializable {
  
    private static final long serialVersionUID = 1L;
  
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "line_id")
    private int lineId;
    @Column(name = "primary")
    private String primary;
    @Column(name = "secondary")
    private String secondary;
    @Column(name = "name")
    private String name;
    @Column(name = "state")
    private String state;
  
  }
  </code></pre>
  
  <p><strong>My state / dictionary repository :</strong></p>
  
  <pre><code>package com.core.app.admin.repository;
  
  import com.core.app.dicts.entity.EDict;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @Repository
  public interface RState extends JpaRepository&lt;EDict, Integer&gt; {
  
    List&lt;EDict&gt; findByPrimary(String primary);
    EDict getByPrimaryAndSecondary();
  
  }
  </code></pre>
  
  <p><strong>My error :</strong></p>
  
  <pre><code>[org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#51933ecb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.core.app.admin.entity.EUser.stateName, referenced property unknown: com.core.app.dicts.entity.EDict.app_dictionaries
  </code></pre>
  
  <p>Thanks in advance.</p>