look at the setting of cascading on the different entities. Usually this happens when you set the Cascade type to ALL on both sides of a relation, which results in saving the entities from both sides of the relation.
Another possibility is when you don't clean up everything nicely, you have an open converation and access the entity in a transactional method. The entity is considered dirty after leaving the method and saved to the database.
In both cases switch on the
showsqlflag, put some logs in the method (or a dirty, but very effective System.out in a PrePersist method) and you see what is happening.
yes, you are right. I got on both side of entities Cascade.ALL
so what do u prefer. I'm not JPA guru. Can u please help me and tell how to set cascading? or disable it at all, but there is nothing in JPA like
What do u suggest?
It all depends on the relation between the entities. Is it a
A is a part of B(a List/Set/Map containing entities), then you should put the cascading Persist/Merge on the
Parent entityand put nothing, or Read on the other side. While you're doing this take care of putting also the FetchType in place. default setting of a many-to-one is eager and one-to-many is lazy. It's always best to let hibernate figure out as much as possible while persisting and updating.
why child should be set as nothing?
If I know default hibernate has refresh as default setting.
How about oneToone? Same like oneToMany?
It all depends on the way you want to persist/merge you objects. You can choose to manage all objects yourself and persist both sides of a relation, or let Hibernate do the job. It all depends on requirememnts, taste etc.
To be honest we shouldn't have this kind of discussion here, but you should go to the Hibernate forum where they are able to answers these answers. The Seam forum can help you but is mainly focusiing on problems in the
upper-layersof your application, assuming you have a sound knowledge of persistency before starting with Seam.