-
1. Re: EM persists entity twice...
lvdberg Jun 18, 2010 6:30 AM (in response to sherkan777)Hi,
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
showsql
flag, put some logs in the method (or a dirty, but very effective System.out in a PrePersist method) and you see what is happening.Leo
-
2. Re: EM persists entity twice...
sherkan777 Jun 18, 2010 7:40 AM (in response to sherkan777)yes, you are right. I got on both side of entities Cascade.ALL
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
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
CascadeType.NONE
What do u suggest?
-
3. Re: EM persists entity twice...
lvdberg Jun 18, 2010 10:00 AM (in response to sherkan777)Hi,
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 theParent entity
and 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.Leo
-
4. Re: EM persists entity twice...
sherkan777 Jun 21, 2010 2:32 AM (in response to sherkan777)why child should be set as nothing?
If I know default hibernate has refresh as default setting.How about oneToone? Same like oneToMany?
-
5. Re: EM persists entity twice...
lvdberg Jun 21, 2010 2:53 AM (in response to sherkan777)Hi Sherkan,
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-layers
of your application, assuming you have a sound knowledge of persistency before starting with Seam.Leo