2 Replies Latest reply on Jul 19, 2008 4:32 PM by Terry

    How to clone entity?

    Terry Apprentice
      is it possible to load entity (record), make changes to few fields, and persist it as a new entity?

      My application uses Seam EntityHome to load entity, I would like to make copy of it change few fields and persist as new entity.

      I guess I can do myentity=new MyEntity(); and set its each field to the one I want to clone from.

      but I'm wondering if there is other neater way, like make clone, set ID to null, change few field as required and persist?

      Seam 2.0.1GA + Hibernate.

        • 1. Re: How to clone entity?
          Guillaume Jeudy Master

          If you clone a loaded hibernate entity you can't assume what you are dealing with exactly. It could be a hibernate proxy class... So for this reason I would stay away from this approach.

          you probably want to create a new instance yourself and copy fields. Now if you don't want to hardcode the copying of fields method calls by method calls, you can probably use reflection to detect getters and call them or hibernate ClassMetaData if you want to focus only on properties that are mapped through hibernate.

          • 2. Re: How to clone entity?
            Terry Apprentice
            Hi thanks for the reply. The hibernate ClassMetaData appears more appropriate for my case, however Im getting exception when it tries to access getter methods for property defiend in entity superclass. I have posted detailed information on hibernate forum.