title: Spring Data JPA with ManyToOne save operation is throwing org.hibernate.PersistentObjectException
body: <p>I'm experimenting  with Spring Data JPA but having issues with saving ManyToOne relationship. </p>
  
  <pre><code>@Entity
  public class Customer {
  
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;
   private String firstName;
   private String lastName;
  
  
   @ManyToOne(cascade=CascadeType.ALL)
   private Address homeAddress;
  
   protected Customer() {}
   // getters and setters ...
  }
  </code></pre>
  
  <p>Many customers can have the same address :</p>
  
  <pre><code> @Entity
   public class Address {
  
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
  
    private String postalCode;
    private String city;
    private String line1;
    private String line2;
    private String country;
  
    public Address() {
      // TODO Auto-generated constructor stub
    }
  
    // getters and setter 
  }
  </code></pre>
  
  <p>CustomerRepository</p>
  
  <pre><code>import org.springframework.data.jpa.repository.JpaRepository;
  
  public interface CustomerRepository extends JpaRepository&lt;Customer, Long&gt;       {
  
  List&lt;Customer&gt; findByLastName(String lastName);
  List&lt;Customer&gt; findByHomeAddress(Address address);
  
  }
  </code></pre>
  
  <p>Application</p>
  
  <pre><code>@SpringBootApplication
  public class Application {
  
  private static final Logger log = LoggerFactory.getLogger(Application.class);
  
  public static void main(String[] args) {
      SpringApplication.run(Application.class);
  }
  
  @Bean
  public CommandLineRunner demo(CustomerRepository repository) {
      return (args) -&gt; {
  
  
  
          Address address1 = new Address("07093", "Brooklyn", "129 67th st", null, "USA");
          Address address2 = new Address("03333", "Qeeens", "333 67th st", null, "USA");
          // save a couple of customers
          Customer jack = new Customer("Jack", "Bauer");
  
          jack.setHomeAddress(address1);
          repository.save(jack);                  
          repository.save(new Customer("Chloe", "O'Brian",address1));
  
   }
  }
  </code></pre>
  
  <p>So I tried to save two customers with the same address I get the following exception: </p>
  
  <pre><code>Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: hello.Address; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: hello.Address
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:299) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.10.5.RELEASE.jar:na]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
  at com.sun.proxy.$Proxy60.save(Unknown Source) ~[na:na]
  at hello.Application.lambda$0(Application.java:35) [classes/:na]
  at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
  ... 6 common frames omitted
  </code></pre>
  
  <p>How can I resolve this issue? you see full code at <a href="https://github.com/suleimana/spring-data-jpa" rel="nofollow noreferrer">this git location</a></p>