-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to Dynamic Selective Update Not Null fields ?
PostPosted: Wed Jan 16, 2008 11:08 am 
Newbie

Joined: Tue Aug 28, 2007 3:18 am
Posts: 13
Hi everyone,

Actually,I'm new to Hibernate.I need a suggestion regarding selectively update fields.

I'm using Spring + Hibernate.
I have a table Emp in Db. I have followed class per table structure and created class Emp. Now class Emp have 4 fields.e.g id,Name,address,age. id is generated by sequence.

If I use hibernateTemplate.saveOrUpdate() for savinf as well as updating.

i> If the record is not present in DB is it inserted.
ii> If the record is already in DB it is updated. Now here is my question.

I have a record of Employee in DB.
id - 34
name- Alex
Address - 23/3 xyz road
age -45

At some point of my application,I have to modify the age and address of this employee.But from service layer I'm getting updated age. All others fields are Null.
I have to update only the age -> I have following ways ---

1. I cant use saveOrUpdate() becuase in that case except age and id field all the other fields will be NULL.
2. I have to retrieve that emp object(dbEmp)
3. map the not null fields of serEmp(i.e Emp from Service layer) to dbEmp.
4.Update the dbEmp.

Is their ANY OTHER way to dynamically update NOT NULL fields of any table despite "calling a retrieve before update " ?

Pleasse Help Me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 11:35 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
have you tried setting the "dynamic-insert" and "dynamic-update" arguments to true? That updates/inserts only the fields you need, not all the row.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 3:02 am 
Newbie

Joined: Tue Aug 28, 2007 3:18 am
Posts: 13
Thanks gonzao,

Actually If I try to update only age and if age is coming from service layer always(i.e age is not null) then I can use HQL [I have used lots of times].

But updation will be failed if age bcomes null.
Becuase in Hibernate Query.setCharracter(0 , age.charvalue()) --> cause NUll pointer exception during runtime. [Actually my concern is regarding updating a character field. Age was just an example]

I have tried dynamic update = true.
But then all teh other field values are becoimg NULL.
From Hibernate point of view it is ok. because Emp object is getting updated in different session (i.e Emp retrieval session is already closed).So it assumed except age all the other field are NULL so they are modified
so it update the row with NULL values.

Can it be done to update only the NOT NULL values to DB during update ?
Using Hibernate Interceptor ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.