-->
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: one-to-one not associating FK to primary PK
PostPosted: Thu Dec 04, 2003 6:00 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
I have two tables
- account table with account_id SERIAL (from sequence) PK
- user_info with id SERIAL (from another sequence) PK AND
with account_id NOT NULL FK referencing account.account_id

I want these table to be related one-to-one through unique FK of user_info table.
I'm using Hibernate 2.1 property-ref feature to get right property association like this:
Account.hbm.xml
--------------------
<one-to-one
name="userInfo"
class="com.simpy.model.UserInfo"
cascade="all"
outer-join="auto"
constrained="false"
property-ref="accountId"
/>

and in UserInfo.hbm.xml I did this
-----------------------------------------
<property
name="accountId"
type="integer"
update="true"
insert="true"
column="account_id"
not-null="false"
unique="true"
/>

<one-to-one
name="account"
class="com.simpy.model.Account"
/>

thus making the association bidirectional.

I also tried this, according to the hibernate documentation on one-to-one association
(hibernate-2.1/doc/reference/html_single/index.html#or-mapping-s1-9)

<property
name="accountId"
type="integer"
update="true"
insert="true"
column="account_id"
not-null="true"
unique="true"
/>

<many-to-one
name="accountId"
class="com.simpy.model.Account"
column="account_id"
unique="true"
update="false"
insert="false"
/>
but neither works

When saving Account object (object for account table) Hibernate
tries to insert a row into user_info without setting
user_info.account_id to point to just inserted account.
The account_id column of user_info table is left empy
----
Account acct = new Account(...);
UserInfo uinfo = new UserInfo(...);
uinfo.setAccount(acct);
acct.setUserInfo(uinfo);

ses.save(account);
-----

If I change UserInfo.hbm.xml, setting accountId property to be not null which is the right thing from the database standpoint
<property
name="accountId"
type="integer"
update="true"
insert="true"
column="account_id"
not-null="true"
unique="true"
/>
I get exception
not-null property references a null or transient value: net.sf.hibernate.persister.EntityPersister.accountId

How to resolve this and tell hibernate to populate FK of user_info table the appropriate way and not just leave it blank?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 6:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In User:

Code:
<many-to-one
    name="account"
    class="com.simpy.model.Account"
    column="accountId"
    not-null="true"
    unique="true"
/


in Account:

Code:
<one-to-one
    name="user"
    class="com.simpy.model.User"
    property-ref="account"
/>


This is what is recommended in the manual.


Top
 Profile  
 
 Post subject: I'm also having trouble
PostPosted: Thu Dec 04, 2003 9:17 am 
Newbie

Joined: Thu Dec 04, 2003 9:07 am
Posts: 7
Location: Johannesburg
I'm also having problems with one-to-one mappings. I've tried the one-to-one mapping that you suggested above. I'm getting the following error :

SEVERE: Error parsing XML: XML InputStream(36)
org.xml.sax.SAXParseException: Attribute "property-ref" must be declared for element type "one-to-one".

I will use a one-to-many mapping till I find a solution for this.

Regards,
Jairaj


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
use Hibernate 2.1


Top
 Profile  
 
 Post subject: that fixed it!
PostPosted: Mon Dec 08, 2003 11:00 am 
Newbie

Joined: Thu Dec 04, 2003 9:07 am
Posts: 7
Location: Johannesburg
Thanks,
Jairaj


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.