title: Hibernate mapping join on one column of constants table
body: <p>I have 2 tables, the first one is quite variable, the second one contains only constants:</p>
  
  <pre><code>USER.ID   USER.NAME   USER.USER_TYPE (FK on USER_TYPE.ID)
  INT       VARCHAR(64) INT(1)
  ----------------------------------
  1         Alex        3
  2         Jane        1
  3         Carl        3
  </code></pre>
  
  <hr>
  
  <pre><code>USER_TYPE.ID   USER_TYPE.VALUE
  INT(1)         VARCHAR(64)
  ------------------------------
  1              PENDING
  2              REGISTERED
  3              BANNED
  4              ACTIVE
  </code></pre>
  
  <p>The foreign key <code>USER.USER_TYPE</code> is required and refering to a primary key <code>USER_TYPE.ID</code> in table <code>USER_TYPE</code> (one-to-one relation). Here is my mapping in Hibernate.</p>
  
  <p>User.java</p>
  
  <pre><code>@Entity
  @Table(name = "USER")
  public class User {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID")
      private int id;
  
      @Column(name = "NAME")
      private String name;
  
      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)   
      @JoinColumn(name = "USER_TYPE") 
      private UserType userType;
  }
  </code></pre>
  
  <p>UserType.java</p>
  
  <pre><code>@Entity
  @Table(name = "USER_TYPE")
  public class UserType {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID")
      private int id;
  
      @Column(name = "VALUE")
      private String value;
  }
  </code></pre>
  
  <p>My goal is to keep the enumerated values in the database. How to map <code>UserType</code>'s value instead of id to <code>User</code> and validate it? I want to pass the constant VALUE to the String instead of its ID.</p>
  
  <pre><code>private String userType;
  </code></pre>
  
  <p>The expected result of the first user would be:</p>
  
  <pre><code>User[id=1, name=Alex, userType=Banned]
  User[id=2, name=Jane, userType=Pending]
  User[id=3, name=Carl, userType=Banned]
  </code></pre>
  
  <hr>
  
  <p>My attempt was to use this annotation on definition of table twice with both colums switched</p>
  
  <pre><code>@SecondaryTable(name="USER_TYPE", 
      pkJoinColumns={@PrimaryKeyJoinColumn(name="ID", referencedColumnName="USER_TYPE")}
  )
  </code></pre>
  
  <p>and get the VALUE with </p>
  
  <pre><code>@Column(table="USER_TYPE", name="VALUE")
  private String UserType;
  </code></pre>
  
  <p>however it leads to the error</p>
  
  <blockquote>
    <p>Unable to find column with logical name: USER_TYPE in org.hibernate.mapping.Table(USER) and its related supertables and secondary tables</p>
  </blockquote>