5 Replies Latest reply on Jun 21, 2010 2:53 AM by lvdberg

    EM persists entity twice...

    sherkan777

      Hi,
      I've simple entity, which I persist to db, something like.




      Members newMember = new Members();
      Members sponsor   = MembersDAO.loadMember(Integer.valueOf(this.sponsorId), getEM());
      newMember.setParent(sponsor);
      getEM().persist(newMember);
      



      Everything works fine, new member is added to db with his parent, but unfortunately yesterday when tester added new member, my EM added two same records to db but with different PK. All other fields are same? this looks like em made mirrored copy to db.
      Question is why, this happen first time.


      Is any possible to check it why this happen? Simple workaround is to add unique index on some column on db, but this is only workaournd. I need to know why this happen, because this is business project.

        • 1. Re: EM persists entity twice...
          lvdberg

          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

            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

              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 the Parent 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

                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

                  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