-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problems mapping too generic legacy database
PostPosted: Wed Feb 08, 2006 4:16 am 
Newbie

Joined: Wed Feb 08, 2006 3:12 am
Posts: 2
Hibernate version: 3.1
Name and version of the database you are using:Oracle 10G

I'm having problems mapping our legacy database schema. The problem is that it's actually created in a too generic way. I'll try to sketch the situation with a stripped down example:

Code:
TABLE OUR_OBJECTS
  columns:
    NUMBER ID
    VARCHAR TYPE
    VARCHAR TITLE

TABLE OBJECT_TYPE1
  columns:
    NUMBER OBJ_ID   (foreign key to OUR_OBJECTS.ID)
    NUMBER EXTRA_INFO
   
TABLE OBJECT_TYPE2
  columns:
    NUMBER OBJ_ID   (foreign key to OUR_OBJECTS.ID)
    VARCHAR SUBTITLE

TABLE OBJECT_LINKS
  columns:
    NUMBER MASTER_OBJ_ID   (foreign key to OUR_OBJECTS.ID)
    NUMBER DETAIL_OBJ_ID   (foreign key to OUR_OBJECTS.ID)


We have 3 types:
    -type1 (join with OBJECT_TYPE1 table)
    -type2 (join with OBJECT_TYPE2 table)
    -type3 (no joins needed)

Relations:
type1 is master of exactly one type2
type1 is master of multiple type3's

Basically this follows the Table per subclass, using a discriminator architecture rather closely.

Mapping documents:
Code:
<class name="ParlObject" table="OUR_OBJECTS" discriminator-value="">
  <id name="id" column="ID" type="java.lang.Long">
    <generator class="sequence">
      <param name="sequence">OBJ_SEQ</param>
     </generator>
   </id>
   <discriminator type="java.lang.String" column="TYPE" force="true"/>
   <property name="title" column="TITLE" type="java.lang.String" />

   <subclass name="Type1" discriminator-value="TYPE1">
     <set name="type3Set" table="OBJECT_LINKS">
       <key column="MASTER_OBJ_ID"/>
       <many-to-many class="Type3" column="DETAIL_OBJ_ID" where="TYPE = 'TYPE3'" />
     </set>
     <join table="OBJECT_LINKS">
       <key column="MASTER_OBJ_ID"/>
       <many-to-one name="type2" column="DETAIL_OBJ_ID" />
     </join>
     <join table="OBJECT_LINKS">
       <key column="MASTER_OBJ_ID"/>
       <many-to-one name="type4" column="DETAIL_OBJ_ID"/>
     </join>

     <join table="OBJECT_TYPE1">
       <key column="OBJ_ID"/>
       <property name="extraInfo" column="EXTRA_INFO">
     </join> 
   </subclass>

   <subclass name="Type2" discriminator-value="TYPE2">
     <join table="OBJECT_TYPE2">
       <key column="OBJ_ID"/>
       <property name="subTitle" column="SUBTITLE">
     </join> 
   </subclass>

   <subclass name="Type3" discriminator-value="TYPE3"> 
   </subclass>

  </class>


The problem I have now is when I load an entity of Type1, I get an exception:
org.hibernate.HibernateException: More than one row with the given identifier was found

This is because when he writes the underlying query he gets multiple possibilities for it's type2's (namely all other type3's)

I fixed the lookup allready a bit by adding a where clause for the type3Set lookup, but there is no where-clause available on a many-to-one...

I also tried specifying to use a fetch="select" on the join for the type2 as well as on the many-to-one of type2, but to no success. He still tries to load the entity as a whole.

Is there a way to better map this schema?

Or is there any chance a where could be added on the many-to-one ? (for any hibernate developers out there)

TIA

PS: I tried replacing the join with a formula for the many-to-one of type2, but performance took a big hit when writing hql with joins because of all hte resulting sub-queries


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 3:35 am 
Newbie

Joined: Wed Feb 08, 2006 3:12 am
Posts: 2
So, no solutions out there?

Did some more testing and generic lookups using the primary key also gave problems with this solution...

More than one row found for reference... :-(

Please, any possibility you devs could add a where clause to the many-to-one?


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