-->
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.  [ 5 posts ] 
Author Message
 Post subject: What is the INSERT in Hibernate?
PostPosted: Tue Mar 10, 2009 8:07 pm 
Newbie

Joined: Sun Feb 08, 2009 10:41 am
Posts: 13
Hi. I have a huge doubt. What is an INSERT in Hibernate? Here is the case study:
I have a User object, which contain String username and String password, and let say List hobbies.
I populate these fields and perform persistence of object User:

Code:


User user = new User();
user.setUserName("boby");
List list = new ArrsyList();
list.add("tennis");
list.add("Golf");
user.setHobbies(list);
user.setPassword("someone");
getHibernateTemplate().saveOrUpdate(user);



Ok. I'm ok with that. But How to get the same effect as using INSERT statement when I use jdbc, in order to insert more hobbies(in this case) into user's list??? When I retrieve user from database, now I want to perform such an update (so the user object is retrived):

Code:

List updatedList = user.getHobbies();
updatedList.add("Some new Hoby");


, now, if I perform
Code:

getHibernateTemplate().saveOrUpdate(user);

the list has been replaced in database, with an old one?! and I do not want that.
If I use
Code:
getHibernateTemplate().save(user);



I get the two users with the same values of username and password?!
So, obviously, 'cause I'm new in Hibernate, I use wrong syntax for accomplishing insertion in database. Where do I wrong and what is the correct way, when I want to update the list of some data of user?
Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:36 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
afrodom wrote:
the list has been replaced in database, with an old one?

What do you mean by that?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 3:38 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Use Session.save() to save a new entity. To update, you don't need to do anything special. Hibernate will automatically detect changes and make modifications to the database when you commit the transaction or when you call Session.flush(). Only use saveOrUpdate() only when you need to associate an entity with a different session than what you used to load it with. In any case, changes are not stored in the database until you are calling commit() or flush(). Here is some more info: http://www.hibernate.org/hib_docs/v3/re ... pdate.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 8:18 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I suppose this topic http://forum.hibernate.org/viewtopic.php?t=995520 is a continuation of this thread. Why have you made a new thread for this?
afrodom wrote:
I've bad expressed, about list replacing. Here it is:

When I retrieve user A, which have id=2 username='test', and some List of staff, and modify that List of stuff by adding some, and perform save() again, then in database, I another user A, with id=2 and a List table, whose join column point at latest user-with id=3!
The first user with id=2, which is origin user, has no List anymore (in other words the List has no more reference id to this user, but the second created one).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 8:56 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
This is because you are using session.save()
save creates a new row in the table. The second time you can use update() which will update the existing entity instead. But if you dont have any way to know if its first or second time then you can use saveOrUpdate() always. This method will save, if the entity is non-existing and update if the entity is existing.

_________________
Regards,
Litty Preeth


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