Hello,
I'm writing a web application using Hibernate (JPA) + Wicket + Spring for my university project, and in certain situation i have detached entity passed to persist exception.
The object i try to persist has colletion of elements (One-To-Many relationship) of which every single one has association to another domain object (Many-To-One relationship) - Examination has collection of Samples, and every Sample is connected with exactly one Patient.
The form for registering new Examinations allows user to insert information about Sample and it's Patient, or load Patient's information from database if he was registered earlier, then add Sample to Examination and either add more samples or register (save to database) an Examination. Everything goes smoothly as long as i keep creating new Patients for every Sample, but when i try to bind a fresh Sample object with Patient loaded from the database i get mentioned exception when trying to persist Examination object.
I walked around this problem using entityManager.merge instead of entityManager.Persist for saving Examination objects, but it keeps bothering me, because I'm not sure if this is neat and safe solution.
I'd be very grateful, if you could suggest me some more elegant solution if there exists one (and as I'm just a begginer in this area, I'm pretty sure it does).
Best regards,
Michal Olejnik
|