title: Detached entity passed to persist in Spring-Data
body: <p>I have two tables in my db <code>Brand</code> and <code>Product</code> with the next simple structure:</p>
  
  <p>| Brand | id PK | </p>
  
  <p>| Product | id PK | brand_id FK |</p>
  
  <p>and entities for that tables:</p>
  
  <pre><code>@Entity
  @Table(name = "Brand")
  public class Brand {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "brand")
      private String brand;
  
      /* getters and setters */
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name = "Product")
  public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "brand_id")
      private Brand brand;
  
      /* getters and setters */
  }
  </code></pre>
  
  <p>As I use Spring-Data I have repository and service with implementation for Brand:</p>
  
  <pre><code>@Repository
  public interface BrandRepository extends JpaRepository&lt;Brand, Long&gt; {
  
      Brand findByBrand(String brand);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface BrandService {
  
      Brand findByBrand(String brand);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Service
  public class BrandServiceImpl implements BrandService {
  
      @Autowired
      private BrandRepository brandRepository;
  
      @Override
      public Brand findByBrand(String brand) {
  
          return brandRepository.findByBrand(brand);
      }
  }
  </code></pre>
  
  <p>and for Product:</p>
  
  <pre><code>@Repository
  public interface ProductRepository extends JpaRepository&lt;Product, Long&gt; {
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface ProductService {
  
      Product save(Product product);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Service
  public class ProductServiceImpl implements ProductService {
  
      @Autowired
      private ProductRepository productRepository;
  
      @Override
      public Product save(Product product) {
          return productRepository.save(product);
      }
  }
  </code></pre>
  
  <p>The goal is to save Product object. Brand object should be saved automatically if it doesn't exist in db or should be set to Product otherwise:</p>
  
  <pre><code>Brand brand = brandService.findByBrand(brandName);
  if (brand == null) {
      brand = new Brand();
      brand.setBrand("Some name");
  }
  product.setBrand(brand);
  productService.save(product);
  </code></pre>
  
  <p>It works fine if Brand object with specified brandName is not in my db. But if it is I get:</p>
  
  <pre><code>PersistentObjectException: detached entity passed to persist
  </code></pre>
  
  <p>for Brand.</p>
  
  <p>I can change cascade type to MERGE and it will work fine. But if I run the code with MERGE cascade type and Brand object with specified brandName is not in my db I get</p>
  
  <pre><code>IllegalStateException:
  org.hibernate.TransientPropertyValueException:
  object references an unsaved transient instance - save the transient instance before flushing
  </code></pre>
  
  <p>for Brand (that's really not surprised).</p>
  
  <p>What Cascade Type should be? Ot what I did wrong?</p>