-->
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.  [ 7 posts ] 
Author Message
 Post subject: another one-to-one mapping question
PostPosted: Fri Feb 27, 2004 9:02 am 
Newbie

Joined: Fri Feb 27, 2004 8:51 am
Posts: 4
Location: London
Hi,

I have two classes: Player and Address. There is a one-to-one association between them. Player has set/getAddress methods.

I have two database tables, one for each. On Player there is a fk which should hold the id of the Address for that Player.

The mappings for each are:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
package="football">

<class name="Address">
<id name="ID">
<generator class="native">
</generator>
</id>
<property name="number" not-null="true"/>
<property name="street" not-null="true"/>
<property name="city" not-null="true"/>
<property name="postCode" not-null="true"/>
</class>

</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
package="football">

<class name="Player">
<id name="ID">
<generator class="native">
</generator>
</id>
<property name="firstName" not-null="true"/>
<property name="lastName" not-null="true"/>
<property name="number" not-null="true"/>
<property name="position" not-null="true"/>
<property name="transferFee" not-null="false"/>
<one-to-one name="address" class="Address" outer-join="auto" constrained="true" foreign-key="ADDRESS_ID"/>
<!-- <many-to-one name="address" class="Address" outer-join="auto" foreign-key="ADDRESS_ID"/> -->
</class>

</hibernate-mapping>

You'll notice that I've commented out the many-to-one tag. I found that when I save an Address and a Player using many-to-one, the fk column in the Player table is updated correctly. When I use one-to-one, the fk column has null.

Any suggestions?

I'm using Hibernate 2.1 with a Pointbase db.

Regards,

Don


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 9:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
try this:
<one-to-one name="address" class="Address" outer-join="auto" constrained="true" property-ref="ID"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 10:49 am 
Newbie

Joined: Fri Feb 27, 2004 8:51 am
Posts: 4
Location: London
Tried that:
MappingException property-ref not found: ID in class: football.Address

There is an ID in Address class and there are get/set methods and Address instances can be saved in the database.

I don't know how using this would allow Hibernate to know that the column ADDRESS_ID in table PLAYER is meant to hold the ID of the Address.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 7:04 pm 
Newbie

Joined: Wed Feb 25, 2004 7:20 pm
Posts: 4
I think it is supposed to find it since the name points to a name (ID) in the Address class. That would be <id name="ID"> in Address... I have tried this aswell but i get the same MappingException as you do...

Sorry cant help you ... just confirm that i have the same problem as you do...

regards,
Kristoffer


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 4:49 am 
Newbie

Joined: Fri Feb 27, 2004 8:51 am
Posts: 4
Location: London
Hi,

It's good to know that it's just not me. I'm tempted to infer that what I'm trying to do, although seemingly very simple, is not possible using Hibernate.

Regards,

Don


Top
 Profile  
 
 Post subject: Re: another one-to-one mapping question
PostPosted: Sat Feb 28, 2004 6:06 am 
Newbie

Joined: Tue Jan 27, 2004 3:26 pm
Posts: 18
donm wrote:
Hi,

I have two classes: Player and Address. There is a one-to-one association between them. Player has set/getAddress methods.

I have two database tables, one for each. On Player there is a fk which should hold the id of the Address for that Player.



It's not one-to-one relation, it's many-to-one.

donm wrote:
Any suggestions?


Read documentation:
http://www.hibernate.org/hib_docs/refer ... pping-s1-9

And this:
http://www.xylax.net/hibernate/

Regards
Artur


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 1:26 pm 
Newbie

Joined: Fri Feb 27, 2004 8:51 am
Posts: 4
Location: London
So, one-to-one where they entities have the same value for primary key is <one-to-one> but where a one-to-one where a fk is used is a <many-to-one>.

And of course in the second one-to-one, using my example, I couldadd the same Address to multiple Person entities, which is not my intent.

I must check what would happen using EJBs and CMR..........


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