• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need clarification for Update in Hibernate

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a parent and child relationship. Both are of type Person. I need to know how to update the child's parent. Currently I am loading the parent and child from the database and then set the parent to the child and update the child. Is there an simple update statement for the same. My current code is

where the named query is


I need query like


But instead of the type String, in my case it is Person. Should I need to load new and old Peron to make a update statement.

Thanks in advance.
K
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

You could use NativeQuery for update data in Hibernate..
perhaps it's simpler than we have to use HQL..
the NativeQuery in Hibernate is executed like this :


you could use the parameters also for dynamic query..
Hope this help..

Correct me if i'm wrong..
Thanks..
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Leo.

I would like to also know if the same can be done using HQL as simple as you posted with NativeSqlQuery.

Thanks again.
K
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about if you try to get the "Object", then set with the new property value, and last update the object through session.update();

this is the steps requred :


Sorry, perhaps in this code any JPQL...
but the logic is same..
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently I am doing that only. Instead of the name, I have another entity that needs to be loaded from the data base. So it is like 2 select statements to load the child and Parent (both of type person). and another statement to update the child with the new parent set in it. There are 3 sql statement executed in the database.

And as you suggest if we are going to use NativeSqlStatement, there is only one sql statement executed in the DB. This should increase the performance.

My doubt here is does using HQL always result in performance hit when compared to native sql.

Thanks
K
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its could be arranged in the fetch property value set to LAZY...
While we set the fetch property to LAZY, then the hibernate force to generate only the "Parent"..

For further information you can read book Java Persistence with Hibernate..
Thanks,..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic