Hi,
I'm working on a web application where I'm currently not able to persist certain entities because I get this exception:
"org.hibernate.PersistentObjectException: detached entity passed to persist: Entities.Person"
For a more complete understanding of the problem I will show you the most relevant code from my entities (They're related to each other in several steps so to see the connections I'm showing you 4 entities):
As you can see, the Cv entity is sort of the "base class" which all the other entities directly or indirectly are connected to.
In my application I let the user upload his or her cv in
Word format. After that I let a session bean extract the names of skills which match a certain list from the cv. Then the found skills are shown to the user, who can edit or delete them and choose values for level and experience. The user can also create new skills.
Nothing is saved to the database until the user is completely satisfied and clicks "save". Then a
servlet is called which reads all the needed information (which includes firstName, lastName and birthDate which the user has filled in, in the "upload cv page") from the current HttpSession object. After that the servlet creates a new Cv and tries to persist it. But when I try to do that I get the earlier mentioned exception:
"Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: Entities.Person"
That is.. I get that exception if the Person object already exists in the database and is retrieved to be connected to the newly created Cv entity.
If I create a new Person everything works well and the new Cv is persisted. But I don't really understand Why it's working..
If the problem is that entities become detached as soon as they have been retrieved from the db and that causes problems when the detached entity is connected to a "new object" which should be persisted, then I think I should have problems in the "new Cv with a new Person" case as well.
I'll explain why I think so:
For each Cv that is to be created I create or retrieve a Person object (as mentioned). But I also create a list of new Skillinstances, and as the code shows each Skillinstance has a connection to a Skill object - which in most cases already exists in the db and is just retrieved. The Skillcategories which the Skill objects are connected to are Always already in the db as well.
So I thought that there Would be a couple of detached Skill and Skillcategory entities, always, regardless of the state of the Person entity. Why doesn't this cause a problem but the Person entity does? And what should I do about it? Should I change the scope of the entity managers (for the moment they "live" inside entity facade session beans. One for each class) and in that case how?
I just want to know the best way to solve this, so if anyone has any good ideas I would be very grateful!
Best regards,
Ylva Degerfeldt