-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: 2.1 Beta 4 and one-to-one mappings
PostPosted: Thu Oct 16, 2003 4:24 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Hi,

I have two classes in a one-to-one relationship but I don't want them to have to have the same PK. So I want to have one have a FK to the other's PK like this:

Code:
CREATE TABLE guide (
id integer primary key
);

CREATE TABLE survey (
id integer primary key,
guide_id integer not null unique,
foreign key(guide_id) references guide
);


In java, I want each class to have a reference to the other class.

In Survey.hbm.xml I have:
Code:
      <many-to-one
         name="guide"
         class="org.etp.Guide"
         column="guide_id"
         not-null="true"
         unique="true"
         cascade="none"
         outer-join="auto"
         />


So far so good. I think I'm using the new feature of 2.1 with the following in Guide.hbm.xml:
Code:
      <one-to-one
         name="survey"
         class="org.etp.Survey"
         foreign-key="guide_id"
         outer-join="auto"
         cascade="delete"
         />


However, Guide.survey is not populated by Hibernate and looking at the SQL generated it looks like the join is still on survey.id=guide.id:

Code:
select _vote0_.id as id__, _vote0_.owner_id as owner_id__, votersur1_.id as id0_, votersur1_.guide_id as guide_id0_, votersur1_.key as key0_, votersur1_.live as live0_, _vote0_.id as id1_, _vote0_.general_date as general_2_1_, _vote0_.primary_date as primary_3_1_, _vote0_.runoff_date as runoff_d4_1_, _vote0_.owner_id as owner_id1_, _vote0_.partner as partner1_, _vote0_.title as title1_, _vote0_.html as html1_ from guide _vote0_ left outer join survey votersur1_ on _vote0_.id=votersur1_.id where _vote0_.owner_id=?


So my first question is whether this is indeed what 2.1 means with the foreign-key attribute of <one-to-one>. And if so why isn't it working?

- Jesse


Top
 Profile  
 
 Post subject: for starters...
PostPosted: Thu Oct 16, 2003 7:14 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Do you really want a one-to-one? If so, why do you have Survey mapped with many-to-one to Guide?

If there are many Surveys for each Guide then you would have a one-to-many from Guide to Survey, not a one-to-one.


Top
 Profile  
 
 Post subject: P.S.
PostPosted: Thu Oct 16, 2003 7:17 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
It looks like you want a bidirectional association so one of them needs to have inverse="true".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Your tables are setup correctly but the foreign key attribute is only a hint for a tool such as hbm2ddl to generate the foreign key line for the schema with a name that you choose. You are missing the property-ref attribute , eg,

<one-to-one
name="survey"
class="org.etp.Survey"
outer-join"auto"
property-ref="guide_id"
/>


Top
 Profile  
 
 Post subject: confusing foreign-key and property-ref
PostPosted: Fri Oct 17, 2003 11:25 am 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Thanks David,

You're right, I just got foreign-key and property-ref mixed up (confusing naming if you ask me). I just needed to use the latter when I used the former. The only think to note is that the property-ref, as the name suggests is the name of the Java class property, not the actual SQL column name. So in my case:

property-ref="guide"


Top
 Profile  
 
 Post subject: Another Problem: no "not-null" ?
PostPosted: Fri Oct 17, 2003 12:35 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
Ok,

So now I have the problem that it doesn't seem to support a one-to-one being not-null="false":

Guide mapping:
Code:
      <one-to-one
         name="survey"
         class="org.etp.Survey"
         property-ref="voterGuide"
         outer-join="false"
         cascade="delete"
         />

Survey mapping:
Code:
      <many-to-one
         name="guide"
         class="org.etp.Guide"
         column="guide_id"
         not-null="true"
         unique="true"
         cascade="none"
         outer-join="false"
         />

The entity relationship is that a survey must have a guide (and is unique over all surveys) but a Guide may have zero or one survey. According to the dtd, the one-to-one element doesn't have a "not-null" attribute. I generate the following SQL:

Code:
select votersur0_.id as id0_, votersur0_.guide_id as guide_id0_, votersur0_.key as key0_, votersur0_.live as live0_ from survey votersur0_ where votersur0_.guide_id=?


and the following exception:
Code:
net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 44671, of class: org.etp.Survey
        at net.sf.hibernate.impl.SessionImpl.throwObjectNotFound(SessionImpl.java:1872)
        at net.sf.hibernate.impl.SessionImpl.loadByUniqueKey(SessionImpl.java:3730)
        at net.sf.hibernate.type.OneToOneType.resolveIdentifier(OneToOneType.java:82)
        at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2161)


Any suggestions?

Gavin, et al: BTW, the message "No row with the given identifier exists: 44671" might be changed to include the name of the column identifier since Hibernate now supports reference properties besides the PK.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 4:40 pm 
Beginner
Beginner

Joined: Fri Aug 29, 2003 12:38 am
Posts: 22
Location: Phoenix, AZ
I am having this exact same problem.. can one of the Hibernate developers confirm this as a bug or explain why it is not?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 4:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I am having this exact same problem.


Same as what??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 1:13 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 12:38 am
Posts: 22
Location: Phoenix, AZ
gavin wrote:
Same as what??

the same as the post directly before where I said "I am having the same problem"! aka cutie's last message Posted: Fri Oct 17, 2003 4:35 pm.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 3:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
not-null makes no sense for a <one-to-one>, since the one-to-one joins on the primary key, which is not null by definition.


Top
 Profile  
 
 Post subject: yeah but ...
PostPosted: Wed Oct 22, 2003 8:46 am 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
gavin wrote:
not-null makes no sense for a <one-to-one>, since the one-to-one joins on the primary key, which is not null by definition.


Yeah but ...

2.1 allows you to have a one-to-one mapping for a FK on another class pointing to the PK on this class. That FK obviously must be unique so that this can be a one-to-one and not a one-to-many. But the FK can also conceptually be null, which means that the one-to-one should be allowed to be not-null="false". But this doesn't seem to be accounted for since an Exception is always thrown if the related entry doesn't exist.


Top
 Profile  
 
 Post subject: yeah but ...
PostPosted: Wed Oct 22, 2003 8:47 am 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
gavin wrote:
not-null makes no sense for a <one-to-one>, since the one-to-one joins on the primary key, which is not null by definition.


Yeah but ...

2.1 allows you to have a one-to-one mapping for a FK on another class pointing to the PK on this class. That FK obviously must be unique so that this can be a one-to-one and not a one-to-many. But the FK can also conceptually be null, which means that the one-to-one should be allowed to be not-null="false". But this doesn't seem to be accounted for since an Exception is always thrown if the related entry doesn't exist.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 1:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
But this doesn't seem to be accounted for since an Exception is always thrown if the related entry doesn't exist.


Ah this is wrong, I think. I made a little two-line change in CVS.

(P.S. this is not the not-null attribute which governs this, it is the constrained attribute.)


Top
 Profile  
 
 Post subject: Re: yeah but ...
PostPosted: Wed Oct 22, 2003 2:03 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
cutie wrote:
But the FK can also conceptually be null, which means that the one-to-one should be allowed to be not-null="false". But this doesn't seem to be accounted for since an Exception is always thrown if the related entry doesn't exist.


Sorry I actually meant that there could possibly not exist a row in the other table such that FK=PK of this table. Therefore the reference from this class to the other class would be null.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 5:35 pm 
Beginner
Beginner

Joined: Fri Aug 29, 2003 12:38 am
Posts: 22
Location: Phoenix, AZ
gavin wrote:
(P.S. this is not the not-null attribute which governs this, it is the constrained attribute.)


but the constrained attribute is documented as
Quote:
constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which save() and delete() are cascaded (and is also used by the schema export tool).

Why cant I have a foreign key constraint yet still allow a null value? this way the constraint is in place if there is a relationship, but not having the relationship is also legal. I would think they should be different properties (constrained/not-null)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.