title: How to map weak entity hibernate
body: <p>I have the following relation in database: <a href="https://i.stack.imgur.com/lJzaV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lJzaV.png" alt="Image"></a></p>
  
  <p>I have one strong table.</p>
  
  <p>I have one weak table that has one to one relation with strong table. Really it's 0 to 1 relation, because strong table doesn't have always one line in weak table. To identify this weak table is enough the Id of strong table.</p>
  
  <p>And finally I have another weak table, with ManyToOne relation with first weak entity. It needs the id of OneToOneWeakEntity (that also is id of strong table), and his own id. It's like an historical of OneToOneWeakTable.</p>
  
  <p>I want to map in Hibernate, but I don't know how to do it.</p>
  
  <p>Now I have the following code:</p>
  
  <pre><code>@Entity
  @Table(name="table")
  public class Table {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="id_table")
      private Integer idTable;
      private String otherAtributes;
  
      ....
  }
  
  
  @Entity
  @Table(name="oneToOneWeakTable")
  public class OneToOneWeakEntity {   
  
      @OneToOne(cascade = CascadeType.ALL, optional=false) 
      @Id
      @JoinColumn(name="table_id_table")
      private Table table;
      private String otherAtributes;
  
      ....
  }
  
  
  @Entity
  @Table(name="oneToManyWeakTable")
  @IdClass(EntityPk.class)
  public class OneToManyWeakTable {
  
      @Id
      @ManyToOne
      @JoinColumn(name="table_id_table")
      private OneToOneWeakEntity oneToOneWeakEntity;
      @Id
      @Column(name="own_id")
      private String ownId;
      private String otherAtributes;
  
      ....
  }
  
  class EntityPk {
  
      @Id
      @ManyToOne
      @JoinColumn(name="table_id_table")
      private OneToOneWeakEntity oneToOneWeakEntity;
      @Id
      @Column(name="own_id")
      private String ownId;
      private String otherAtributes;
  
      ....
  </code></pre>
  
  <p>}</p>
  
  <p>My problem is when I try to run my application, because I have this deployment error:</p>
  
  <pre><code>Caused by: org.hibernate.AnnotationException: A Foreign key refering package.OneToOneWeakEntity from package.OneToManyWeakTable has the wrong number of column. should be 0
  at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:502)
  at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:117)
  at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1518)
  at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
  </code></pre>
  
  <p>How can I map this relation??</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>: I also try with this to map OneToOneWeakTable:</p>
  
  <pre><code>@OneToOne(cascade = CascadeType.ALL, optional=false) 
  @PrimaryKeyJoinColumn
  @Id
  @JoinColumn(name="table_id_table")
  private Table table;
  </code></pre>
  
  <p>In this case, I also have an error when I try to deploy, but a different exception:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
  at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:568)
  at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:258)
  at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
  at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1518)
  at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
  </code></pre>