-->
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.  [ 2 posts ] 
Author Message
 Post subject: Can Hibernate resolve transient objects during saveOrUpdate?
PostPosted: Thu Jun 01, 2006 12:32 pm 
Beginner
Beginner

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
Hibernate version: 3.1

Is there a way to get Hibernate to resolve transient objects to persistent objects when saveOrUpdate is invoked?

Example:

TABLE: user_type
id INT PK
name VARCHAR2(20)

Record: 1, administrator
Record: 2, user

TABLE: user
id INT PK
username VARCHAR2(20)
password VARCHAR2(20)
user_type_id INT FK(user_type.id)

class UserType
{
public Integer id;
public String name;
//setter/getters...
}

class User
{
Integer id;
public String username;
public String password;
public UserType userType;
//setter/getters...
}

class ExampleService
{
Session session; //set arbitrarily

public void test()
{
User user = new User();
user.setUsername("jdoe");
user.setPassword("password");

// I want this transient UserType to be resolved to the persistent UserType with the same 'name' property when session.saveOrUpdate is invoked. Right now saveOrUpdate always creates a new record in the database.
UserType userType = new UserType();
userType.setName("administrator");

user.setUserType(userType);

session.saveOrUpdate(user);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 4:26 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
I Don´t think this is what the hibernate documentation says about saveOrUpdate()


Code:
saveOrUpdate() does the following:
• if the object is already persistent in this session, do nothing
• if another object associated with the session has the same identifier, throw an exception
• if the object has no identifier property, save() it
• if the object's identifier has the value assigned to a newly instantiated object, save() it
• if the object is versioned (by a <version> or <timestamp>), and the version property value is the same
value assigned to a newly instantiated object, save() it
• otherwise update() the object


Since your object has no identifier it wil always create a new one
you would have to do a search in your table to see if the user exists....

_________________
Don´t forget to rate!


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