views:

29

answers:

1

When saving an entity using Spring Forms and Hibernate I use the following controller method (simplified) to do that.

@Transactional
@RequestMapping(value="/speichern", method = RequestMethod.POST)
public void saveEvent(final @ModelAttribute("__eventData") MDKEvent p_event) {
     em.persist(p_event);
} // saveEvent

When I try to edit it using the same method I get an "deteched entity" exception:

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: some.package.Entity
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1174)
    ...

It seams that the entity is in unmanaged state after submitting the form (which makes sense). How could I save the updated entity anyway? What is the correct way for doing this?

I'm using Spring 3 and Hibernate 3.5.

+1  A: 

em.merge(p_event) will work. I got a different error and didn't see that it's doing basically what I'm looking for.

Jan