I have this simple situation:
@Entity
public class Customer{
@ManyToMany(fetch=FetchType.EAGER)
@Cascade(CascadeType.SAVE_UPDATE)
private List<Product> products=new ArrayList<Product>();
}
@Entity
public class Produtc{
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private Category category;
}
@Entity
public class Category{
private String name;
}
Routine to insert a new customer:
Customer customer=new Customer();
//customer.set data
Product p=dao.getProductBySomeUserInput...
if(p==null){
p=new Product();
//p.set data
}
customer.addProduct(p);
dao.save(customer); //this row throw NonUniqueObjectException on Category class
How can i resolve this issue? I think the problem is related to CascadeType.SAVE_UPDATE, but i need it... Thanks all.
UPDATE
I found the problem and this is how to replicate it:
Customer c=new Customer();
// Load two products by id
Product p=dao.get(Product.class, 2);
Product p1=dao.get(Product.class, 3);
c.addProduct(p);
c.addProduct(p1);
// This try to update products, and category of products becouse CascadeType is SAVE_UDPATE
dao.save(c);
So, if p and p 1 has different categories there is no probs, but if p and p1 has same category i have NonUniqueObjectException on Category becouse same category is in session and hibernate try to save_update its.
I need CascadeType.SAVE_UPDATE in both Product and Category entities, so how can i resolve this issue? Thanks.