4 Replies Latest reply on Jan 28, 2010 12:57 AM by bsgcic

    Use of instance of entity without involvement of hibernate?

    bsgcic

      Could someone please help me with the following question?


      Suppose I have the following entity with a corresponding table, columns, and data:


      @Entity
      @Name("myName")
      public class MyName implements Serializable
      {
       private Integer id;
       private String firstName;
       private String lastName;
       (Getters & Setters)
      }
      



      Now suppose in a conversation scoped action bean, I create two instances of myName: fromDbName and nonDbAlteredName.


      I set fromDbName equal to a single result of an entityManager.createQuery statement. Say id=1, firstName="John", lastName="Doe"


      I then set nonDbAlteredName equal to fromDbName. (But with no intention of doing anything with the database for nonDbAlteredName. Want to just alter some values within nonDbAlteredName and use that within the action bean.)


      No errors thus far.


      Then, I attempt to change nonDbAlteredName.lastName = "Smith" and I get a hibernate error.


      Is there a way to declare nonDbAlteredName of type MyName but with it designated to have nothing to do with the database or hibernate. In other words, is there a way to use the fields and methods of MyName for nonDbAlteredName but without it being considered as a database entity? Or do I need to create a non-entity version of MyName with identical fields and methods just so that I can freely updated it without hibernate getting involved?


      Your insight would be greatly appreciated.


      Regards,


      Jeff


        • 1. Re: Use of instance of entity without involvement of hibernate?
          blabno

          Jeff, of course you can use objects annotated with @Entity without causing any database actions. Changes to entities are being sent to database only if you have connected them with EntityManager before (find,merge,etc.). Show your source code so we can give you more precise directions.

          • 2. Re: Use of instance of entity without involvement of hibernate?
            bsgcic

            Bernard,


            Thank you very kindly for the help.
            Your guidance saved me from continuing down a path of a lot of extra unneeded work. I was in the process of creating duplicate non entity classes because of this issue until I read your response. The project is time critical so that was really a help.


            I found that a significant mistake was in the way I was trying to copy the fromDbName instance as a new instance called nonDbAlteredName. I had just set nonDbAlteredName = aNameFromDb which just referred nonDbAlteredName to the same instance (aNameFromDb being a parameter of a subroutine receiving fromDbName).


            Below is my alternate attempt at doing this where I am assigning each value of aNameFromDb to the corresponding field of nonDbAlteredName.


            Do you know if there is an easier way to do this? Here is my current solution attempt:


            
            MyName fromDbName = new MyName();
            Populate fromDbName via entityManager.createQuery statement
            
            performCalculation(fromDbName);
            
            public performCalculation(MyName aNameFromDb) {
             MyName nonDbAlteredName = new MyName();
             
             for (Field myNameField: nonDbAlteredName.getClass().getDeclaredFields()) {
            
               myNameField.setAccessible(true);
               try {
                 myNameField.set(nonDbAlteredName, myNameField.get(aNameFromDb));
               }
               catch statements
             }
            
            



            Thanks again


            Regards,


            Jeff

            • 3. Re: Use of instance of entity without involvement of hibernate?
              blabno

              Jeff, you obtain a copy of object by on of the following:



              1. serializing and deserializing it (by hand or using org.apache.commons.lang.SerializationUtils.clone(Serializable))

              2. implementing clone() method on your entity (it's contract is described in Object class javadoc) and call it

              3. write custom method that will use setters and getters to copy properties

              • 4. Re: Use of instance of entity without involvement of hibernate?
                bsgcic

                Bernard,


                Thank you very much for all your help.
                I am studying the 3 methods that you instructed.


                Regards,


                Jeff