Hi everybody!
I'm relativly new to
Hibernate version: 3.2, but used hibernate for doing a project a long time ago. I wanted to reverse engineer a database using the hibernate tools and stumbled across the following problem.
I have two tables that are in a many-to-one relation, but they are not connected through primary-keys. To illustrate my problem:
Table 1:
Primary key: pk
Fields: fk,...
Table 2:
Primary key: pk1, pk2
Fields: ...
Table 1 is at the one side of the relation, which means that many entries in table 2 are referenced by field t2.pk1 in Table 1.
The problem is, that i had to manually add the foreign key element to the reveng.xml and somehow the generated mappings don't work properly, because the query for table 2 always uses the primary key of table 1 instead of the field fk in table 1.
heres the excerpt of the reveng.xml:
Code:
<table catalog="Test" name="table1">
<primary-key>
<key-column name="pk" />
</primary-key>
</table>
<table catalog="Test" name="table2">
<primary-key>
<key-column name="pk1" />
<key-column name="pk2" />
</primary-key>
<foreign-key constraint-name="fk_table1" foreign-table="table1" >
<column-ref local-column="pk1" foreign-column="fk" />
<many-to-one property="getFrom" exclude="false" />
<set property="theRows" exclude="false" />
</foreign-key>
</table>
the generated table1.hbm.xml:
Code:
<hibernate-mapping>
<class name="com.test.Table1" table="table1" catalog="Test">
<id name="pk" type="int">
<column name="pk" />
<generator class="assigned" />
</id>
...
<set name="theRows" inverse="true">
<key>
<column name="pk1" not-null="true" />
</key>
<one-to-many class="com.test.Table2" />
</set>
</class>
</hibernate-mapping>
the generated table2.hbm.xml:
Code:
<hibernate-mapping>
<class name="com.test.Table2" table="table2" catalog="Test">
<composite-id name="id" class="com.test.Table2Id">
<key-property name="pk1" type="int">
<column name="pk1" />
</key-property>
<key-property name="pk2" type="short">
<column name="pk2" />
</key-property>
</composite-id>
<many-to-one name="getFrom" class="com.test.Table1" update="false" insert="false" fetch="select">
<column name="pk1" not-null="true" />
</many-to-one>
...
</class>
</hibernate-mapping>
Till here everything looks fine as far as i could judge it, but if i try to select the set from Table1 using a row that surely has entries in Table2 i get a set containing nothing.
Having activated the loggers i found out, that the key assigned to the query, which should be the Table1.fk field is actually Table1.pk. So Table2.pk1 is searched for Table1.pk which is surely not entered there.
Does anybody have any suggestions for me?
I already tried to add the attribute "property-ref" to the many-to-one-element, as the following is stated in chapter 5.1.10. of the reference:
Quote:
The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is an ugly relational model. For example, suppose the Product class had a unique serial number, that is not the primary key. (The unique attribute controls Hibernate's DDL generation with the SchemaExport tool.)
I moreover can not alter the database as i only have reading access.
Thanks for any help in advance.
Kind regards,
jiri