aspose file tools*
The moose likes Object Relational Mapping and the fly likes Help me to Update only selected Fields and not all the columns in database through hibernate ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Help me to Update only selected Fields and not all the columns in database through hibernate ??" Watch "Help me to Update only selected Fields and not all the columns in database through hibernate ??" New topic
Author

Help me to Update only selected Fields and not all the columns in database through hibernate ??

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hi.....iam working on a project....using struts as front-end and hibernate for OR mapping with my sql server 2005
thr are columns such as ein, age, city, name,gender. I want only age and city to get updated but all the fields are getting updated. i don't know where i went wrong. please help me to figure out this issue.
Following is action class i have written:-


_______________________________________________________________________________________________________
In above code... iam trying to update only age and city with ein will be used as WHERE condition.
In my console of Eclipse all the fields are getting updated.
like this
Hibernate: update EmpInfo set Name=?, Password=?, Gender=?, City=?, Age=?, Date=? where Emp_Id=?

One more issue......how can i prevent duplicate EIN from being insterted by the User since its a Primary Key. It throws error on console whenever duplicate EIN tries to get inserted.
Thanks in advance........
JAVA IS GREAT


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi Vinod,

Pleace use code tag when your question contains code.

read this Use Code Tags ..


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi Vinod,

Look at your code,



if you want to update only age and city. But you also update Ein field. And set all three value with new value. And without setting up new value it's not been updated.

So it looks like only




Now do one thing, from your jsp page you get your Ein to your action. Now get select query where id=Ein. Through this you get you database value with all old value information.

Now you can update this populated bean instead.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377



I'm confused, isn't this code pointless, you are setting the update values to the values that they already are...

In the Hibernate Property File you can set whether individual will be updated or not. From docs - update="true|false" .

Sean


I love this place!
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hi Nishan Patel
Thnx for your suggestion
I know in a plain SQL, i can append WHERE condition also to Update that particular record only with desired columns to be updated.
If we talk abt hibernate properties how and where i can implement that WHERE clause.
Can you show some example.
Thank you...
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hi Sean Clark
i did not get you actually...



I want to Update only City and Age. Iam passing Ein because i thought it will automaticaly take ein in WHERE clause.
Then i had commented these line like:-


and put update="true|false" in the <property> tag like :-


but still it is updating all the fields.
Hibernate: update EmpInfo set Name=?, Password=?, Gender=?, City=?, Age=?, Date=? where Emp_Id=?

Can you explain me with an example.

Thank you in advance.....
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

I am not taking about plain SQL.

Either you can use hibernate Criteria .



something like this...

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,
you would actually set update="false" on the fields that you didn't want to update. In your case I don't think that would be advisable as you would then never be able to update the password or gender.

The reason it updates all is that hibernate has no way of knowing if any of the fields have changed (it would need to do a select first to get the data, which is less efficient), so it updates all.

If it is very important that only some fields update I'd say to load/get the object using the primary key and then update the fields that you wish to update.

Sean
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Sean Clark wrote:Hey,
you would actually set update="false" on the fields that you didn't want to update. In your case I don't think that would be advisable as you would then never be able to update the password or gender.

The reason it updates all is that hibernate has no way of knowing if any of the fields have changed (it would need to do a select first to get the data, which is less efficient), so it updates all.

If it is very important that only some fields update I'd say to load/get the object using the primary key and then update the fields that you wish to update.

Sean


I think this two statement contradict to each other.. can you explain what you want to say about

(it would need to do a select first to get the data, which is less efficient)


and

I'd say to load/get the object using the primary key and then update the fields that you wish to update.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

(it would need to do a select first to get the data, which is less efficient)

What I'm saying is that if hibernate was only going to update certain fields i.e. the fields that have changed, then it would need to read the data from the database first compare and update the fields that have changed.

I'd say to load/get the object using the primary key and then update the fields that you wish to update.

Here I am advising the op that this may be a solution to his problem - to load the row from the database update the fields that he wants to change and update.

They are not contradicting, but saying why hibernate is not just updating certain fields and one way that will get him around that.

I'm not saying it is the best or only solution, but it is a solution nonetheless.

Sean
Miral Makadia
Greenhorn

Joined: Sep 04, 2009
Posts: 7
Hi


First you should separate your action code to data access object code. It makes more easier to understand.

Try this code


public BmCustomer edit(BmCustomer cust)
{
BmCustomer sessioncust=null;
Session session = HibernateSessionFactory.getSession();
System.out.println("customerid-->edit-->"+cust.getCustomerid());
sessioncust = (BmCustomer)session.get(BmCustomer.class,cust.getCustomerid());
sessioncust=FormConverter.domainTodomain(sessioncust, cust);
session.beginTransaction();
try{
session.update(sessioncust);
session.getTransaction().commit();
session.flush();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
}
session = null;
return sessioncust;

}
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hey Sean Clark ....you are awesome dude....
Its working well now
i have done:-


set the update="false" to those fields which i don't wana to get updated in their<property> tags.
and see the result:-
Hibernate: update EmpInfo set City=?, Age=? where Emp_Id=?
thank you very much..

However i Appreciate really all you guys who tried to help me.....Thank you every body...
Now i will try to update through some new ideas suggested by Nishan Patel to use hibernate Criteria which i want to learn first

Thank you once again

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help me to Update only selected Fields and not all the columns in database through hibernate ??