title: JPA: Fixed value join columns in collection of elements
body: <p>I would like to reduce the total number of similar tables in my database by using a fixed identifier in each join.</p>
  
  <p>Currently my entity class looks like this:</p>
  
  <pre><code>public class Group {
  
      [...]
  
      @ElementCollection(fetch = FetchType.LAZY)
      @CollectionTable(
              name = "group_admins",
              joinColumns = @JoinColumn(name = "group_id"))
      @Column(name = "name", unique = true, nullable = false)
      @OrderBy
      private Set&lt;String&gt; admins = new LinkedHashSet&lt;&gt;();
  
      @ElementCollection(fetch = FetchType.LAZY)
      @CollectionTable(
              name = "group_moderators",
              joinColumns = @JoinColumn(name = "group_id"))
      @Column(name = "name", unique = true, nullable = false)
      @OrderBy
      private Set&lt;String&gt; moderators = new LinkedHashSet&lt;&gt;();     
  
      @ElementCollection(fetch = FetchType.LAZY)
      @CollectionTable(
              name = "group_user",
              joinColumns = @JoinColumn(name = "group_id"))
      @Column(name = "name", unique = true, nullable = false)
      @OrderBy
      private Set&lt;String&gt; users = new LinkedHashSet&lt;&gt;();
  
      [...]
  
  }
  </code></pre>
  
  <p>And this causes it to create 1+3=4 tables. 3 of them share the exact same columns. Now I would like to reduce this to 1+1=2 tables like that: <a href="https://docs.oracle.com/html/E13946_05/ref_guide_mapping_notes_nonstdjoins.html" rel="nofollow noreferrer">https://docs.oracle.com/html/E13946_05/ref_guide_mapping_notes_nonstdjoins.html</a></p>
  
  <pre><code>    @ElementCollection(fetch = FetchType.LAZY)
      @CollectionTable(
              name = "person",
              joinColumns = {
                      @JoinColumn(name = "group_id", referencedColumnName="id"),
                      @JoinColumn(name = "person.type", referencedColumnName="'user'")
              })
      @Column(name = "name", unique = true, nullable = false)
      @OrderBy
      private Set&lt;String&gt; users = new LinkedHashSet&lt;&gt;();
  </code></pre>
  
  <p>Which I expect to create a table like this:</p>
  
  <ul>
  <li>Person
  
  <ul>
  <li>group_id: int</li>
  <li>type: varchar [either of: 'user', 'moderator', 'admin']</li>
  <li>name: varchar</li>
  </ul></li>
  </ul>
  
  <p>But I always end up with an exception similar to this one:</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: Unable to find column with logical name: 'user' in group
      at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:858) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:243) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1611) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1352) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:810) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:735) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
  </code></pre>
  
  <p>It does not even try to execute the SQL.</p>
  
  <p>I also tried @DescriminatorColumn, but that does not even apply to attributes.</p>
  
  <p>Is there a way to do that without using an extra entity/embeddable for that?</p>
  
  <p>I use </p>
  
  <ul>
  <li>Java 8</li>
  <li>Hibernate 5.2.12</li>
  <li>Spring Boot</li>
  </ul>