-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Only update non-null values in a transient instance
PostPosted: Fri Apr 13, 2007 12:35 pm 
Newbie

Joined: Fri Apr 13, 2007 12:25 pm
Posts: 17
Is there any way to set up a hibernate mapping strategy such that i can create an instance of the mapped class, and only set a few parameters, then when I save it to the database, only update those fields that are not null in my mapped class object? I am writing a loosely coupled application which sends objects to the web layer in JSON format, and I don't want some of the more sensitive fields to be exposed to the client, but i need them to be mapped fields on the business logic layer. As it is right now, if I omit these sensitive fields from my JSON objects (essentially DTO's), the values are overwritten with "null" when I write the objects back to the Hibernate session. Any help you could provide would be awesome.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 14, 2007 11:00 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
One solution might be to create a second Hibernate object that has the fields needed by your DTO. Your business logic layer could be a sub-class of the the "DTO-Entity" containing the sensitive fields. This way, if the parent class won't muck with the fields from the specialization.

Another feature that maybe useful to you is enabling dynamic-update="true" on the class element of your mapping file. Or, if you using annotations, add the following:

Code:
@org.hibernate.annotations.Entity(dynamicUpdate = true,)

This tells Hibernate to generate a SQL statement for only those values that have changed.

You could also manually generate the HQL that updates the specific properties:

http://www.hibernate.org/hib_docs/v3/re ... tch-direct

Anything else would have to managed by you as only you know what field need to be truly updated.



Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.