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: Mapping a legacy database with constraints
PostPosted: Wed Apr 16, 2008 4:51 am 
Newbie

Joined: Wed Apr 16, 2008 4:15 am
Posts: 4
Location: Netherlands
Hi,
I have a problem mapping a legacy database. In the database is a construction to force a maps to construction

The class model

Code:
Class A          Class C
           
Class B          Class D

Class A has a many-to-many relation with class C
Class A has a one-to-many relation with class B
Class C has a one-to-many relation with class D
Class D maps to class B

Database

Code:
Table A        Table C
         Table V
Table B        Table D

Table V is the many-to-many table between table A and table C
Table B has a table_a_id for the on-to-many reference
Table V has a generated id(?), table_a_id and table_c_id
Table D has a table_v_id reference

First I tried to map the class model straight forward with some sql commands for the fetch and a stored procedure to save but I got some problems with the way hibernate writes the data; it first writes the properties (the one-to-many) and then the sets (the many-to-many) and that is not allowed because the database enforces the maps to constraint.

the idea I had then was to create a 'virtual' ClassV the will be created dynamically allowing the class model to stay intact with the many to many relation between A and C. the consequence is that the class model for hibernate looks like this

Code:
Class A          Class C
           ClassV           
Class B          Class D

Class A has a one-to-many relation with class V
Class C has a one-to-many relation with class V
Class A has a one-to-many relation with class B
Class V has a one-to-many relation with class D

when I try this I get the error

NHibernate.MappingException: Foreign key in table D must have same number of columns as referenced primary key in table V

this is kind of logical because table A and table C map to table V and hibernate obviously concludes that the references to tables A and C will be the primary key. But in reality table V has another generated id as the primary key. Argh.

Is it possible to map something like this with hibernate and what would be the best approach?

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
1.2.0
Mapping documents:
Code:
<class name="ClassA" table="A">
    <id name="SystemId" column="Id" type="Int32">
      <generator class="sequence">
        <param name="sequence">GENERATEID</param>
      </generator>
    </id>
    <property name="Description" column="DESCRIPTION" />
    <set name="VSet" cascade="all-delete-orphan">
      <key column="A_ID"/>
      <one-to-many class="ClassV"/>
    </set>
    <set name="BSet" cascade="all-delete-orphan" inverse="true">
        <key column="A_ID" />
        <one-to-many class="ClassB" />
      </set>
  </class>
<class name="ClassC" table="C">
    <id name="SystemId" column="Id" type="Int32">
      <generator class="sequence">
        <param name="sequence">GENERATEID</param>
      </generator>
    </id>
    <property name="Description" column="DESCRIPTION" />
    <set name="VSet" cascade="all-delete-orphan">
      <key column="A_ID"/>
      <one-to-many class="ClassV"/>
    </set>
  </class>
<class name="ClassV" table="V">
   <id name="Id" column="ID" type ="Int32">
      <generator class="sequence">
        <param name="sequence">GENERATEID</param>
      </generator>
    </id>
    <set name="ClassDSet" cascade="all-delete-orphan">
      <key column="V_ID" />
      <one-to-many class="ClassD" />
    </set>
</class>
<class name="ClassD" table="D">
   <id name="Id" column="ID" type ="Int32">
      <generator class="sequence">
        <param name="sequence">GENERATEID</param>
      </generator>
    </id>
</class>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
2008-04-16 10:21:19,078 [10] ERROR NHibernate.Cfg.Configuration [(null)] - NHibernate.MappingException: Foreign key in table D must have same number of columns as referenced primary key in table V
at NHibernate.Mapping.ForeignKey.set_ReferencedTable(Table value) in C:\backup oude laptop\dev\nhibernate\NHibernate-1.2.0.GA-src\src\NHibernate\Mapping\ForeignKey.cs:line 69
at NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done) in C:\backup oude laptop\dev\nhibernate\NHibernate-1.2.0.GA-src\src\NHibernate\Cfg\Configuration.cs:line 1023
Name and version of the database you are using:
Oracle 10G 2
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html[quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 5:50 am 
Newbie

Joined: Wed Apr 16, 2008 4:15 am
Posts: 4
Location: Netherlands
I solved it by changing the database.
thanks for reading


Top
 Profile  
 
 Post subject: Solved
PostPosted: Wed Apr 23, 2008 5:51 am 
Newbie

Joined: Wed Apr 16, 2008 4:15 am
Posts: 4
Location: Netherlands
I solved it by changing the database.
thanks for reading


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.