-->
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: create/insert if doesnt exist.
PostPosted: Tue Jul 20, 2010 3:58 pm 
Newbie

Joined: Wed Jul 07, 2010 6:33 pm
Posts: 6
I am trying to insert an object mapped to a table. The table has some foreign keys.

For example

Table USER {
name char(20),
age int,
address varchar(500),
job_id int,
Foreign key job_id references someothertable.field
);

now when I want to create a new user, i may want to create a new job along with it. So in the USER object I fill in the required details of the user and the new job to be created. Saving this object should create a newuser, and the new job. How do I do that ?


Top
 Profile  
 
 Post subject: Re: create/insert if doesnt exist.
PostPosted: Wed Jul 21, 2010 7:17 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
I guess you must add cascade="save-update" to your many-to-one in User class mapping.
Code:
<class name="User">
  ...
  <many-to-one name="job" column="job_id" class="Job" cascade="save-update" />
  ...
</class>
(I hope I guessed the names, otherwise change them)
However, I usually avoid these situations as I feel I'm loosing control of persisted objects.

Bye
Stefano


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.