title: Cascading entity save with composite key throws NullPointerException
body: <p>I have this relationship structure in database, that I cannot change:</p>
  
  <pre><code>------------------------
  | Person               | 
  ------------------------
   id : pk              
  ------------------------
  
  ------------------------
  | PersonAddress        | 
  ------------------------
   person_id: pk, fk    
   address_id: pk, fk
   address_type 
  ------------------------
  
  ------------------------
  | Address              | 
  ------------------------
   id: pk    
  ------------------------
  </code></pre>
  
  <p>I am trying to configure these relations with JPA and Hibernate annotations.
  One of the ways I've tried to do was this one:</p>
  
  <pre><code>@Entity
  class Person {
  
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "person_id")
    private List&lt;PersonAddress&gt; addresses;
  }
  
  @Entity
  class PersonAddress {
  
    @ManyToOne
    @JoinColumn(nullable = false)
    private AddressType type;
  
    @EmbeddedId
    private PersonAddressId id;
  }
  
  
  @Embeddable
  class PersonAddressId implements Serializable {
  
    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;
  
    @ManyToOne(cascade = CascadeType.ALL)
    @MapsId("id")
    @JoinColumn(name = "address_id", nullable = false)
    private Address address;
  
  }
  
  @Entity
  public class Address {
  
    @Id
    @GeneratedValue
    @Column(name = "address_id")
    private Long id;
  }
  </code></pre>
  
  <p>When i do <code>repository.save(person)</code>. I expect that changes would cascade down to the address.</p>
  
  <p>The problem is that, when I try to save, I get NullPointerException. While debugging I found out, that Hibernate tries to save PersonAddress object, and it needs hash code of one of the primary keys it uses.</p>
  
  <p>Since Address is not yet save, it has id == null.  Basically the flow of save is currently:</p>
  
  <pre><code>Person -&gt; PersonAddress -&gt; Address
  </code></pre>
  
  <p>But it should be:</p>
  
  <pre><code>Person -&gt; Address -&gt; PersonAddress
  </code></pre>
  
  <p>(because it needs to get generated ids for person and adddress).</p>
  
  <p>Is there a way to configure Hibernate to behave in that way?</p>