title: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException
body: <p>My application has two entity Product and Category. When add product , user choose the category that product belong to. But when save the product to data base i meet the error</p>
  
  <blockquote>
    <p>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation</p>
  </blockquote>
  
  <p>the content of entity is 
  Category.java</p>
  
  <pre><code>@Entity
  @Table(name="category")
  public class Category {
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  
  @Column(name="category_name")
  private String categoryName;
  
  @Column(name="description")
  private String description;
  
  @OneToMany(mappedBy = "category", fetch=FetchType.LAZY)
  private Set&lt;Product&gt; products;
  
  //getter and setter method
  }
  </code></pre>
  
  <p>Product.java</p>
  
  <pre><code>@Entity
  @Table(name="product")
  public class Product {
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  
  @Column(name="name")
  @NotNull
  @NotEmpty
  private String name;
  
  @Column(name="description")
  private String description;
  
  @Column(name="manufacture")
  private String manufacture;
  
  @Column(name="price")
  private long price;
  
  @Column(name="image_name")
  private String imageName;
  
  @ManyToOne
  @JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
  Category category;
  
  //getter and setter method
  }
  </code></pre>
  
  <p>ProductDAO.java</p>
  
  <pre><code>@Repository("ProductDAO")
  @Transactional
  public class ProductDAO {
  
  @PersistenceContext
  private EntityManager entityManager;
  
  public EntityManager getEntityManager() {
      return entityManager;
  }
  
  public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
  }
  
  public void create(Product product) {
      entityManager.merge(product.getCategory());
      entityManager.persist(product);
  }
  //other method
  }
  </code></pre>
  
  <p>my jsp file</p>
  
  <pre><code>&lt;form:form method="post" commandName="product" action="add.html" enctype="multipart/form-data"&gt;
      Productname &lt;form:input path="name"/&gt;
                  &lt;form:errors path="name" class ="error"/&gt;&lt;br&gt;
      description    &lt;form:input   path="description"/&gt;&lt;br&gt;
      price    &lt;form:input   path="price"/&gt;&lt;br&gt;
      manufacture &lt;form:input path="manufacture"/&gt;&lt;br&gt;
      Category
      &lt;form:select path="category"&gt;
      &lt;form:options items="${categories}" itemValue="id" itemLabel="categoryName" /&gt;
      &lt;/form:select&gt;
      &lt;form:errors path="category" class ="error"/&gt;&lt;br&gt;
      &lt;br/&gt;
      Product image&lt;input type="file" name="file" /&gt;
      &lt;form:errors path="imageName" class ="error"/&gt;&lt;br&gt;
      &lt;input type="submit" class="register" value="Save"&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p>my Converter</p>
  
  <pre><code>public class CategoryConverter implements Converter&lt;String, Category&gt;{
  
      @Autowired
      private CategoryService categoryService;
  
      public Category convert(String id) {
          return categoryService.findById(Long.valueOf(id));
      }
  }
  </code></pre>
  
  <p>thank for any help</p>