2 Replies Latest reply on Oct 13, 2005 4:24 AM by Krister Wicksell

    Enteties duplicated when merged

    Krister Wicksell Newbie

      I have an entety bean calles ZVersion that has a one-to-many relationship with an entety been called ZProperty.

      If i detatch an instance of ZVersion, add some ZProperty instances and then merge the ZVersion instance. The ZProperty instances will be duplicated in the ZVersion instance. They are however not duplicated in the database.

      Is this a knows issue?

      TIA!
      /Krister

        • 1. Re: Enteties duplicated when merged
          Jonn Beames Newbie

          If I am reading the docs correctly, this may be by design.

          From http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/objectstate.html#d0e706:

          Merging vs. saveOrUpdate/saveOrUpdateCopy

          Merging in EJB3 is similar to the saveOrUpdateCopy() method in native Hibernate. However, it is not the same as the saveOrUpdate() method, the given instance is not reattached with the persistence context, but a managed instance is returned by the merge() method.


          I believe this means we end up with a detached, modified instance and a managed, merged instance. I could be wrong. I'm just beginning to scratch the surface of EJB3 and I believe that the docs I've found are still in "preview" state.

          Hopefully someone authoritative can chime in?
          Jonn

          • 2. Re: Enteties duplicated when merged
            Krister Wicksell Newbie

            Hi John and thanks for your answer.

            I don't think it can be meant to work like this. Maybe you misunderstood my short description of the problem. I have created a test case that you can find at http://www.zert.se/ejb-test-20051013.zip.

            The test creates an instance of ZVersion, modify it localy and then merge it.

            Below is an extract of the test client:

             ...
             ZRepository repository = (ZRepository) context.lookup(ZRepository.class.getName());
            
             ZVersion v = repository.create();
             v.addProperty("a").setValue("1");
             v.addProperty("b").setValue("1");
             v.addReference("a").setValue("1");
             v.addReference("b").setValue("1");
            
             System.out.println("pre update");
             System.out.println("properties:");
             for (ZProperty p: v.getProperties()) {
             System.out.println(p.getIdentifier() + " = " + p.getValue());
             }
             System.out.println("references:");
             for (ZReference r: v.getReferences()) {
             System.out.println(r.getIdentifier() + " = " + r.getValue());
             }
            
             v = repository.update(v);
             System.out.println("\npost update");
             System.out.println("properties:");
             for (ZProperty p: v.getProperties()) {
             System.out.println(p.getIdentifier() + " = " + p.getValue());
             }
             System.out.println("references:");
             for (ZReference r: v.getReferences()) {
             System.out.println(r.getIdentifier() + " = " + r.getValue());
             }
            
             v = repository.get(v.getId());
             System.out.println("\npost get");
             System.out.println("properties:");
             for (ZProperty p: v.getProperties()) {
             System.out.println(p.getIdentifier() + " = " + p.getValue());
             }
             System.out.println("references:");
             for (ZReference r: v.getReferences()) {
             System.out.println(r.getIdentifier() + " = " + r.getValue());
             }
             ...
            


            The output is:
            pre update
            properties:
            a = 1
            b = 1
            references:
            a = 1
            b = 1
            
            post update
            properties:
            a = 1
            b = 1
            references:
            a = 1
            b = 1
            
            post get
            properties:
            a = 1
            a = 1
            b = 1
            b = 1
            references:
            a = 1
            b = 1
            a = 1
            b = 1
            


            I would however expect the output to be:
            pre update
            properties:
            a = 1
            b = 1
            references:
            a = 1
            b = 1
            
            post update
            properties:
            a = 1
            b = 1
            references:
            a = 1
            b = 1
            
            post get
            properties:
            a = 1
            b = 1
            references:
            a = 1
            b = 1
            


            /Krister