title: How to cascade persist a @ManyToOne relation (TransientPropertyValueException)?
body: <p>I want to create a DB mapping using <code>hibernate</code>: each <code>Person</code> may only have one single <code>Address</code>, but the same address may be assigned to multiple persons (eg gamily members).</p>
  
  <p>The following mapping does not work:</p>
  
  <pre><code>@Entity
  public class Person {
      @ManyToOne
      @JoinColumn(name="fk_address_id", foreignKey = @ForeignKey(name="fk_addres"))
      private Address address;
  }
  
  
  @Entity
  public class Address {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String city;
      private int zip;
      private String street;
  
      @OneToMany(mappedBy = "address", cascade = {CascadeType.ALL})
      private Set&lt;Person&gt; persons;
  }
  
  
  @Transactional
  public void create() {
      Person person = new Person();
      Address address = new Address();
      address.setStreet("test 1");
      address.setCity("test");
      address.setZip(123);
      person.setAddress(address);
      dao.save(person);
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : Person.address -&gt; Address
  </code></pre>
  
  <p>Why am I getting this exception, and how can I prevent it? I thought due to the cascading the address should automatically get saved?</p>