-->
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: Mapping a foreign-key on the far end of a many-to-many
PostPosted: Tue Nov 08, 2005 1:41 pm 
Newbie

Joined: Tue Nov 08, 2005 1:35 pm
Posts: 3
I have three tables as follows

Code:
OBJECT TABLE

create table OBJECT(
  OBJECT_KEY int primary key
);



EXPANSION TABLE

create table EXPANSION(
  SEQUENCE int,
  SUPERFIELD_KEY int,
  LEXICON_KEY int,
  OBJECT_KEY int,
  LEXICON_TERM_KEY int,
  PRIMARY KEY (superfield_key, sequence)
);



ALT_TERMS TABLE

create table ALT_TERMS(
  ALT_TERMS_KEY int primary key,
  LEXICON_KEY int
);




Here is the object mapping

Code:
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping  default-cascade="none" auto-import="true"
                   package="org.thf.herman.beans">
  <class name="AObject" table="OBJECT" lazy="true">
    <id name="object_key" column="OBJECT_KEY">
      <generator class="assigned" />
    </id>

    <bag name="altTerms" lazy="true" table="EXPANSION">
      <key column="OBJECT_KEY"/>
      <many-to-many class="AltTerm" column="LEXICON_TERM_KEY" foreign-key="LEXICON_KEY"/>
    </bag>
  </class>
</hibernate-mapping>




and finally the mapping for ALT_TERM

Code:
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping  default-cascade="none" auto-import="true"
                   package="org.thf.herman.beans">
  <class name="AltTerm" table="ALT_TERMS" lazy="true">
    <id name="alt_terms_key" column="ALT_TERMS_KEY">
      <generator class="assigned" />
    </id>
    <property name="lexicon_key" column="LEXICON_KEY" type="long"/>
    <property name="term" column="TERM" type="string" />
    <property name="authority" column="AUTHORITY" type="string" />
    <property name="differentiator" column="DIFFERENTIATOR" type="string" />
  </class>
</hibernate-mapping>





When I load an object the alt_term sql is as follows:

select altterm0_.ALT_TERMS_KEY as ALT_TERM1_0_ from ALT_TERMS altterm0_ where altterm0_.ALT_TERMS_KEY=?



when I want

select altterm0_.ALT_TERMS_KEY as ALT_TERM1_0_ from ALT_TERMS altterm0_ where altterm0_.LEXICON_KEY=?



Hibernate seems to default to the primary key for the far end of the many-to-many relation even though I inserted a foreign-key in the mapping. I have tried foreign-key in several different places and still not got around this problem. Is there a way of getting a many-to-many when the far end of the relationship is not the primary key?

Any light shed on this most appreciated.

Regards

Jonathan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 2:36 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
Can you post the HQL that gave you the *wrong* SQL?


Top
 Profile  
 
 Post subject: HQL that generates *wrong* sql
PostPosted: Tue Nov 08, 2005 2:44 pm 
Newbie

Joined: Tue Nov 08, 2005 1:35 pm
Posts: 3
Is this what you mean?

Code:
AObject ao = session.load(AObject.class, new Long(123));
List terms = ao.getAltTerms();


Top
 Profile  
 
 Post subject: Re: HQL that generates *wrong* sql
PostPosted: Tue Nov 08, 2005 7:18 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
jmoore85 wrote:
Code:
AObject ao = session.load(AObject.class, new Long(123));
List terms = ao.getAltTerms();

If that's how you load the AltTerm collection, then of course Hibernate will do select using the PK ALT_TERMS_KEY. You should look into using a HQL query if you really want to find AltTerms based on LEXICON_KEY. For example:

Code:
List terms = session.createQuery("from AltTerm at where at.lexiconKey = :key")
                      .setInteger("key", atObject.getLexiconKey())
                      .list();


Top
 Profile  
 
 Post subject: many-to-many equivalent of one-to-one property-ref
PostPosted: Tue Nov 08, 2005 9:27 pm 
Newbie

Joined: Tue Nov 08, 2005 1:35 pm
Posts: 3
Thanks that works. I was hoping for a many-to-many equivalent of the one-to-one property property-ref

This allows me to do roughly similar with a 1:1 relationship.

Can you point me to a description of what the foreign-key property on a many-to-many is for? It doesn't seem to alter the SQL in any way.

Thanks in advance.


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.