Hi, I'm using Hibernate Annotations 3.2.0.CR1 with Entity Manager 3.2.0.CR1.
I'm having a problem when I update (merge) a object with new added child itens. It seems that hibernate is calling merge for the child itens too put it should have called persist because the itens are new and are not persisted yet.
When I merge the object I get the following exception:
javax.persistence.EntityNotFoundException: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: program.CustomerContact#0
And the hibernate does a sql update and not a insert for the new child item.
The code:
Code:
class Customer
{
(.....)
@OneToMany(mappedBy = "customer", cascade= { CascadeType.ALL } )
@Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN} )
@OnDelete(action = OnDeleteAction.CASCADE)
public List<CustomerContact> getContacts()
{
if ( contacts == null ) {
contacts = new ArrayList();
}
return contacts;
}
public void addContact(CustomerContact c)
{
c.setCustomer(this);
this.contacts.add(c);
}
}
The code that adds the child item:
Code:
Customer o=em.find(Customer.class,0);
CustomerContact cc = new CustomerContact();
cc.setName("Carl");
o.addContact(cc);
em.merge(o);
Then the merge is called for the new CustomerContact object. And I get a EntityNotFoundException.
How can I avoid that?
Thanks in advance.
MaurĂcio.