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