-->
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: Child needs Parent, but Parent not inserted (inserse=t/f?)
PostPosted: Wed Dec 10, 2003 10:56 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hello,

(I have a feeling this has something to http://hibernate.org/155.html and inverse="true/false", but would appreciate guidance with adding inverse='true/false" to the correct side of the relation)

Summary of the problem:
Saving one domain model POJO does not save (insert) another, required
domain model POJO.

Details:
2 tables: ACCOUNT & USER_INFO
2 domain model POJOs: Account & UserInfo

When saving UserInfo to USER_INFO table, no Account is inserted into
ACCOUNT table.


-- ACCOUNT:

id SERIAL
CONSTRAINT pk_account_id PRIMARY KEY,
username ....

-- USER_INFO:

id SERIAL
CONSTRAINT pk_user_info_id PRIMARY KEY ,
account_id INTEGER
CONSTRAINT fk_user_info_account_id REFERENCES account(id) ,
first_name ....


-- ACCOUNT.hbm.xml:

<one-to-one
name="userInfo"
class="com.example.model.UserInfo"
cascade="all"
outer-join="auto"
constrained="false"
property-ref="account"
/>

-- USERINFO.hbm.xml:

<many-to-one
name="account"
class="com.example.model.Account"
column="account_id"
not-null="true"
unique="true"
/>


-- JAVA CODE that does not 'do the right thing':

Account acct = getAccountFromSomewhere();
UserInfo uinfo = new UserInfo("John", ....);
uinfo.setAccount(acct);
// IMPORTANT: if I add the following line, then Account IS saved
// acct.setUserInfo(uinfo);
Session ses = getHibernateSessionFromSomewhere();
ses.save(uinfo);
ses.flush();
ses.connection().commit();
ses.close();


Thank you,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 11:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please read http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue first and tell me if it answers your questions.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 11:59 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Hello Emmanuel,

Well, I read that document before posting (see the 155.html reference at the very top of my post). However, it looks like I have many-to-one, which is implicilty and invariably inverse="false" (quote from 155.html: "Note that <many-to-one> is always inverse="false" (the attribute does not exist).")

Does inverse="true/false" still apply here? That is, where would I have to place it for Account to get INSERTed before UserInfo?

Thanks,
Otis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Sorry I read your post too fast.
add cascade="save-update" in your many-to-one. Hibernate will then cascade account creation when saving user.

_________________
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.