-
1. Re: Use of instance of entity without involvement of hibernate?
blabno Jan 26, 2010 10:30 AM (in response to bsgcic)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 Jan 27, 2010 3:34 AM (in response to 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 Jan 27, 2010 9:13 AM (in response to bsgcic)Jeff, you obtain a copy of object by on of the following:
- serializing and deserializing it (by hand or using org.apache.commons.lang.SerializationUtils.clone(Serializable))
- implementing clone() method on your entity (it's contract is described in Object class javadoc) and call it
- write custom method that will use setters and getters to copy properties
-
4. Re: Use of instance of entity without involvement of hibernate?
bsgcic Jan 28, 2010 12:57 AM (in response to bsgcic)Bernard,
Thank you very much for all your help.
I am studying the 3 methods that you instructed.Regards,
Jeff