title: Hibernate. Associate two tables with one by one key column
body: <p>I have legacy database with 3 tables: ENTITY1, ENTITY2, PROPERTY. Each entity have several properties. Both entity tables bounded with property-table by entity_id key which is located in property table. So I haven't real constraints.</p>
  
  <p>DB scheme:<p>
  <img src="https://i.stack.imgur.com/spJY9.png" alt=""></p>
  
  <p>I prefer to store all properties in Map in Entity1-class so I must have many-to-one assotiation in Property-class (I haven't founded other solutions. <strong>Are they exist?</strong>).</p>
  
  <p><strong>So how can I associate Entity2-class with property-class?</strong></p>
  
  <p>Enity1.class:</p>
  
  <pre><code>@Entity
  @Table(name = "ENTITY1")
  public class Entity1 {
  
      @Id
      @Column(name = "ENTITY_ID")
      private long id;
  
      @Column(name = "some_field")
      private String field;
  
      @OneToMany(mappedBy = "entityId")
      @MapKey(name="key")
      private Map&lt;Long, Property&gt; properties;
  }
  </code></pre>
  
  <p>Property.class</p>
  
  <pre><code>    @Entity
      @Table(name = "PROPERTY")
      public class Property {
  
          @Id
          @Column(name = "property_id")
          private long id;
  
          @Column
          private long key;
  
          @Column
          private String value;
  
  //are there other solutions?
          @ManyToOne
          @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID")
          private Entity1 entity1;
      }
  </code></pre>
  
  <p>Entity2.class skeleton</p>
  
  <pre><code>@Entity
  @Table(name = "ENTITY1")
  public class Entity2 {
  
      @Id
      @Column(name = "ENTITY_ID")
      private long id;
  
      @Column(name = "some_field")
      private String field;
  
      // What can I do?
      private Map&lt;Long, Property&gt; properties;
  }
  </code></pre>
  
  <p>Can't remove @ManuToOne annoutation from Property.class couse of next exception:</p>
  
  <pre><code>11:38:40,825 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 72) MSC000001: Failed to start service jboss.persistenceunit."sudir-gateway-ear-0.0.1-SNAPSHOT.ear#primary": org.jboss.msc.service.StartException in service jboss.persistenceunit."sudir-gateway-ear-0.0.1-SNAPSHOT.ear#primary": java.lang.NullPointerException
         at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
         at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
         at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_51]
         at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474)
         at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_51]
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_51]
         at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]
         at org.jboss.threads.JBossThread.run(JBossThread.java:122)
  Caused by: java.lang.NullPointerException
         at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1460) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:107) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
         at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
         at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:]
         at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
         ... 8 more
  </code></pre>