-->
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: one-to-many mappding ignoring foreign key
PostPosted: Wed Jun 28, 2006 5:21 am 
Newbie

Joined: Mon Jun 26, 2006 4:19 am
Posts: 3
I have posted this question in the NHibernate forum a few days ago, but since it's not .Net specific (in my opinioin) I'm posting it here again, hoping I will get more feedback.

Here's what I have: a ResourceString table with an Id (not very meaningful) and a ResourceId (Guid) which forms a secundary key with the field IsoLangCode.
Most entity tables (like 'Model' in the example below) link to this resource table with a XxxResId field.

The following hbm.xml contains a on-to-many mapping for a property called Descriptions in the Model class.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="X.Core.Data.Model, X.Core.Data" table="model" lazy="false">
        <id name="Name" column="`Name`" type="String">
            <generator class="assigned" />
        </id>
        <property name="DescriptionResId" type="Guid" />

        <bag name="[b]Descriptions[/b]" table="ResourceStrings" lazy="false" >
            <key column="ResourceId" foreign-key="DescriptionResId" />
            <one-to-many class="X.Core.Data.ResourceString, X.Core.Data" />
        </bag>
        <bag name="Features" table="Model2Feature" lazy="false">
            <key column="ModelName" />
            <many-to-many class="X.Core.Data.Feature, X.Core.Data" column="FeatureName" />
        </bag>
    </class>
</hibernate-mapping>


As you can see, I have set 'DescriptionResId' as the foreign-key. When I run my getter-code I get an exception, though. I've noticed that the generated SQL is OK, except for the where clause parameter. The parameter is assigned the Name of the Model instance (the PK of the Model table) and not the DescriptionResId I asked NHibernate to use.

Looking at the NHibernate source made me suspect that the foreign-key attribute is ignored for one-to-many (and simpel collection mapping) mappings... (N)Hibernate will always use the PK.

Am I correct, or just missing the obvious?

Thanks,

G.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 6:05 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
foreign-key is the wrong attribute. You want property-ref. Note that property-ref takes the name of the hibernate property in question, not the DB column name.

Have a glance at the ref docs for what foreign-key does.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 3:07 am 
Newbie

Joined: Mon Jun 26, 2006 4:19 am
Posts: 3
Thanks for your reply!

I jus tried it out, but got an exception stating: The 'property-ref' attribute is not declared. Which was what I expected, since the xsd didn't allow it in the first place. Appearantly, the property-ref attribute can be used in a one-to-one tag, not in a one-to-many.

I'm a real newbie when it comes to (N)Hibernate and having a hard time to believe that what I'm trying to accomplish is not possible - it's not like I'm doing something extra-ordinary here.

Maybe I should use a whole other approach?

G.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 5:42 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
property-ref goes on <key>, not <one-to-many>. I had a look in the Hibernate 2.1 DTD (I think that's equivalent to what's used by NHibernate) and it's not there, so I guess that this is an Hibernate 3 feature. Looks like you're out of luck, sorry..

_________________
Code tags are your friend. Know them and use them.


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.