• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinod,

Pleace use code tag when your question contains code.

read this Use Code Tags ..
 
Nishan Patel
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not taking about plain SQL.

Either you can use hibernate Criteria .



something like this...

 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
knowledge is the difference between drudgery and strategic action -- tiny ad
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic