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.
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.
posted 11 years ago
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.
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.
posted 11 years ago
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.
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter