-->
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.  [ 3 posts ] 
Author Message
 Post subject: Reverse engineering, foreign-key column-ref ignored?!
PostPosted: Fri Dec 08, 2006 7:16 am 
Newbie

Joined: Thu Dec 09, 2004 9:04 am
Posts: 3
Hi!

As I'm relativly new to Hibernate version: 3.2 I have posted to the wrong list I think.

I have written my question under the same topic in the hibernate users forum, but as I have looked around for some time now I think it would be more appropriate to issue it here.

http://forum.hibernate.org/viewtopic.php?t=968096&highlight=


Thanks for any help.

_________________
greets
jiri


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 8:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so do the post here instead and i'll answer

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 10:24 am 
Newbie

Joined: Thu Dec 09, 2004 9:04 am
Posts: 3
Hi!

So here is the problem as posted in the other forum:
I am using Hibernate 3.2 and want to reverse engineer an existing 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:
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.

_________________
greets
jiri


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.