title: Bidirectional @OneToMany relation with JPA annotations doesn't seem to work
body: <p>This is in reference to this <a href="https://stackoverflow.com/questions/27671776/how-to-make-a-one-to-many-bidirectional-relation-with-jpa-annotations-where-the">answer</a>.</p>
  
  <p>Entities-</p>
  
  <p>// Many to One</p>
  
  <pre><code>@Entity
  @Table
  public class Address {
  
      @Id
      @GeneratedValue
      @Column
      private int addressIdentity;
  
      @Column
      private int houseNo;
  
      @Column
      private char streetNo;
  
      @Column
      private int pincode;
  
      @Column
      private String city;
  
      @Column
      private String state;
  
      @Column
      private String country;
  
      @ManyToOne
         @JoinTable(name="PersonAddress", 
              joinColumns=@JoinColumn(name="addressId", insertable = false, updatable = false),
              inverseJoinColumns=@JoinColumn(name="personId", insertable = false, updatable = false)
         )
      private Person person;
      // getters and setters
  </code></pre>
  
  <hr/>
  
  <p>One to Many</p>
  
  <pre><code>@Entity
  @Table
  public class Person {
  
      @Id
      @GeneratedValue
      @Column
      private int personId;
  
      @Column
      private String name;
  
      @Column
      private String designation;
  
      @OneToMany
      @JoinTable(name = "PersonAddress", 
                              joinColumns = @JoinColumn(name = "personId"), 
                                  inverseJoinColumns = @JoinColumn(name = "addressId"))
      private Set&lt;Address&gt; addSet = new HashSet&lt;Address&gt;();
      // getters and setters
  </code></pre>
  
  <p><hr/>
  Hibernate configuration file-</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory name=""&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;hello&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:postgresql://localhost:5432/xyz&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;postgres&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
          &lt;mapping class="ManyToOne_OneToManyMappingWithJoinTable.Person" /&gt;
          &lt;mapping class="ManyToOne_OneToManyMappingWithJoinTable.Address" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <hr/>
  
  <p>the persistence logic-</p>
  
  <pre><code>        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
          Session session = sessionFactory.openSession();
          session.beginTransaction();
  
          Person person1 = new Person();
          person1.setName("Shahnaz Parveen");
          person1.setDesignation("HouseWife");
  
          Address address1 = new Address();
          address1.setHouseNo(18);
          address1.setStreetNo('E');
          address1.setPincode(250002);
          address1.setCity("Meerut");
          address1.setState("UP");
          address1.setCountry("INDIA");
          address1.setPerson(person1);
  
          Address address2 = new Address();
          address2.setHouseNo(84);
          address2.setStreetNo('1');
          address2.setPincode(250002);
          address2.setCity("Meerut");
          address2.setState("UP");
          address2.setCountry("INDIA");
          address1.setPerson(person1);
  
          person1.getAddSet().add(address1);
          person1.getAddSet().add(address2);
  
          session.save(address1);
          session.save(address2);
          session.save(person1);
  
          session.getTransaction().commit();
          session.close();
  </code></pre>
  
  <p><hr/>
  I am getting -</p>
  
  <pre><code>Jan 07, 2017 9:47:35 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
  WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
      Unsaved transient entity: ([ManyToOne_OneToManyMappingWithJoinTable.Person#0])
      Dependent entities: ([[ManyToOne_OneToManyMappingWithJoinTable.Address#1]])
      Non-nullable association(s): ([ManyToOne_OneToManyMappingWithJoinTable.Address.person])
  Exception in thread "main" org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : ManyToOne_OneToManyMappingWithJoinTable.Address.person -&gt; ManyToOne_OneToManyMappingWithJoinTable.Person
      at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:122)
      at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:418)
      at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:621)
      at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:684)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
      at ManyToOne_OneToManyMappingWithJoinTable.ManyToOne_OneToManyMappingWithJoinTableImpl.main(ManyToOne_OneToManyMappingWithJoinTableImpl.java:40)
  </code></pre>
  
  <p>It works perfect with hbms.</p>
  
  <p>Please suggest. </p>
  
  <hr>
  
  <p>Thanks Vlad and Neil, it works but there is a problem described below-</p>
  
  <p>This is the structure which gets created with HBMs. Hence the same must be with Annotations.</p>
  
  <pre><code>CREATE TABLE person_address
  (
    addressid integer NOT NULL,
    personid integer NOT NULL,
    CONSTRAINT person_address_pkey PRIMARY KEY (addressid , personid ),
    CONSTRAINT fkkpp6mysmnyiywx3q33yxr1gbe FOREIGN KEY (personid )
        REFERENCES person (person_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT fkrpk0jx2y558su288tx9kd5cs6 FOREIGN KEY (addressid )
        REFERENCES address (address_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION
  )
  </code></pre>
  
  <p>the moment I do - </p>
  
  <pre><code>@OneToMany(cascade = CascadeType.ALL, mappedBy = "person")
  private Set&lt;Address&gt; addSet = new HashSet&lt;Address&gt;();
  </code></pre>
  
  <p>the join table structure is-</p>
  
  <pre><code>CREATE TABLE personaddress
  (
    personid integer,
    addressid integer NOT NULL,
    CONSTRAINT personaddress_pkey PRIMARY KEY (addressid),
    CONSTRAINT fkfd5pm843bldj10y5kxwo37xge FOREIGN KEY (addressid)
        REFERENCES address (addressidentity) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT fkjuwlthwsi53bpf902nnl6snxh FOREIGN KEY (personid)
        REFERENCES person (personid) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION
  )
  </code></pre>
  
  <p>You see that the primary key is <strong>NOT a combination of addressid and personid</strong> as in HBMs. Please suggest.</p>