-->
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.  [ 3 posts ] 
Author Message
 Post subject: Simple one-to-one association - Very weird problem
PostPosted: Tue Jan 18, 2005 6:33 pm 
Newbie

Joined: Thu Jan 06, 2005 6:27 pm
Posts: 10
Extremely wierd problem I am facing. My test setup is very simple: one-to-one association between user and address. Both tables use the uid column as the PK.

[b]Mapping documents:[/b]
<class name="test.User" table="user">

<id name="uid" column="uid" type="long">
<generator class="assigned"/>
</id>

<property name="first_name" column="first_name" type="string"/>
<property name="last_name" column="last_name" type="string"/>

<one-to-one name="address" cascade="all" class="test.Address" outer-join="auto" constrained="false" />

</class>

<class name="test.Address" table="address">

<id name="uid" type="long" column="uid" unsaved-value="null">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>

<property name="street" column="street" type="string"/>

<one-to-one name="user" class="test.User" outer-join="auto" constrained="true" />

[b]Code between sessionFactory.openSession() and session.close():[/b]
User u = new User();
u.setUid(200);
Address a = new Address();
a.setUid(200);
a.setUser(user);
u.setAddress(a);

There are 2 issues:

1. Hibernate is trying to update the address table instead of inserting. I've tried many things (played around with unsaved-value, etc.) but can't get it to insert.

2. It's using the wrong columns for the update to the address table !! It is using the columns for the user table. Following is what it tries to do:

update address set first_name = ?, last_name = ? where uid = ?

Obviously, the database complains that there is no column called first_name in the address table. I have gone over my mapping files many times to make sure I am not doing something silly but I just can't find what's wrong. As you can see from the above mapping, it is a very simple one-to-one association.

Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 7:07 pm 
Newbie

Joined: Thu Jan 06, 2005 6:27 pm
Posts: 10
I haved fixed issue 2 (wrong column names used), but still can't get address to be inserted, rather than updated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 11:45 pm 
Beginner
Beginner

Joined: Thu Dec 04, 2003 3:47 am
Posts: 31
Location: Hong Kong
I wonder if it is the problem of how Hiberante differentiate between transient and persistent object? since both user and address use uid as PK, I suppose the key for address is "assigned" and not "identifier". Aslo check unsaved=? in ur hbm mapping file for the key of the address. When you try to persist the address object (I suppose you have assigned the key value), hibernate recognizes it as persisted object, so it would call "update" sql instead of "insert" sql. so try to use session.save instead of session.saveorupdate?


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