Forums Register Login

defeinive copies and hibernate?

+Pie Number of slices to send: Send
in all the recommendations i read that an object should private a defensive copy if it's data. (like unmodifiable collections), how does it go , with hibernate strategy will it force me to use field level access to all properties?

i've recently came across this while trying to clone a hibernate object model and my getters returned unmodifiable collections.
+Pie Number of slices to send: Send
Howdy Greenhorn!

Indeed, if you have a POJO with restricted access to its collections, well, then you're going to have a hard time accessing that restricted collection.

I'm not sure what the strategy is here. Basically, someone only wants you to have read-only access? If that's all you've got, well, then that's all you've got.

What's the bigger issue here? Is the issue that you want to do this yourself and you're wondering how, or are you just wondering why this was done in the first place?

Personally, I can't see the justification. Perhaps the collection was simply lazy loaded. When you do a clone, you only get a shallow copy - the deeper references don't get copied. Maybe what you were seeing wasn't a defensive data strategy, but instead, a shallow copy of an object.

-Cameron McKenzie
+Pie Number of slices to send: Send
i'm interested on how this is done in generael, i'm using beanlib's ability to clone objects so this is a deep copy, however, it uses getters and setters to decide about cloning, but it cannot create a new instance unmodiable collection (naturally) nor can it actualy fill, it would have been helpfull it was based on hibernate's mapping metadata, but it can't.
+Pie Number of slices to send: Send
I might wonder why you're cloning this way?

You can pull a POJO out of Hibernate with a get or load. Once you've got the data you need, you can 'evict' the object from Hibernate. It is now the entire object, but it is a 'detached instance' and Hibernate doesn't know about it. Essentially, you can think of it as a clone of your data, floating on the JVM, but no associated with the Hibernate Session, so if you mess around with the object, it won't get persisted, unless of course, you pass it to the saveOrUpdate method of the Session.

Just an idea. And it's probably an idea I've come up with because I'm not too sure how the beanutils are cloning your data.

-Cameron
+Pie Number of slices to send: Send
didnt' say beanutils, said beanlib

nice idea about detachment, only one problem, i need all the id's to be turned to unfertilized values so hibernate won't know it's the same object, i also need the ENTIRE domain domain model being replicated, so something needs to traverse it.
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1000 times.
Similar Threads
Readonly unmodifiable collection
Regarding the FINAL qualifier and arrays...
Immutable and unmodifiable difference
Why final is not applied for Map ?
Unmodifiable vs Immutable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:00:58.