Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

defeinive copies and hibernate?

 
Elhanan Maayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Elhanan Maayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Elhanan Maayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic