-->
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.  [ 4 posts ] 
Author Message
 Post subject: saveOrUpdate does not save new objects/rows
PostPosted: Fri Nov 21, 2003 2:48 pm 
Newbie

Joined: Fri Nov 21, 2003 12:49 pm
Posts: 2
I'm writing a web application and am unable to get Hibernate to save new rows when using saveOrUpdate(). Existing records are updated as expected.

Here is the important part of my config file:
<hibernate-mapping>
<class name="com.datamodel.User" table="users">
<id name="id" type="integer" column="id" unsaved-value="null">
<generator class="identity">
<param name="type">integer</param>
</generator>
</id>
...

And here is the code to save/update an object of this type:

public String saveUser(User user)
{
String errorStr = null;
Session session = null;
try
{
session = sessionFactory.openSession();

Transaction transaction = session.beginTransaction();

session.saveOrUpdate(user);
transaction.commit();
session.close();
} catch (HibernateException he) {
errorStr = he.getMessage();
log.error("Unable to save user " + user.getUserId(), he);
}
return errorStr;
}

Any help would be appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 6:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
set unsaved-value correctly


Top
 Profile  
 
 Post subject: How?
PostPosted: Sun Nov 23, 2003 3:29 pm 
Newbie

Joined: Fri Nov 21, 2003 12:49 pm
Posts: 2
I have unsaved-value set according to the Hibernate documentation as far as I can tell. I'm not sure what you mean. Any more detail would be greatly appreciated.

gavin wrote:
set unsaved-value correctly


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 23, 2003 3:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In your case (unsaved-value=null) user will be saved (inserted) if User.id is null value.
Is user id an object (Integer, Long, etc) ? If yes, it must be null.
If not (primary type), then you must supply an appropriate unsaved value (0 for example).

_________________
Emmanuel


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