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: composite keys and simple reference using many-to-one
PostPosted: Thu Jun 22, 2006 4:45 am 
Newbie

Joined: Thu Jun 22, 2006 4:04 am
Posts: 8
Location: Prague
Hello
Here is my problem...
Im creating mappings for read only access to legacy database which uses composite keys extensively.
Following schema is used for "connecting" TV_DEVICEs to one TV_UNIT:

Code:
TABLE TV_DEVICE (
  HOUSE_ID int not null,
  TV_DEVICE_ID int not null,
....some other fields
) PRIMARY KEY (HOUSE_ID, TV_DEVICE_ID)

TABLE TV_UNIT (
  HOUSE_ID int not null,
  TV_UNIT_ID int not null,
  ACTIVE_FROM datetime not null,
  TV_DEVICE_ID_1 int,
  TV_DEVICE_ID_2 int,
  TV_DEVICE_ID_3 int,
) PRIMARY KEY (HOUSE_ID, TV_UNIT_ID, ACYIVE_FROM)

I map this tables like this:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Mandysoft.TvDevice,Mandysoft" table="TV_DEVICE">
      <composite-id access="field">      
         <key-many-to-one name="House" class="Mandysoft.Houses, Mandysoft" column="HOUSE_ID" />
         <key-property name="TvDeviceID" column="TV_DEVICE_ID" type="Int32" />
      </composite-id>
      
      <property ..........      
   </class>
</hibernate-mapping>


and :

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Mandysoft.TvUnit,Mandysoft" table="TV_UNIT">
      <composite-id access="field">      
         <key-many-to-one name="House" class="Mandysoft.Houses, Mandysoft" column="HOUSE_ID" />
          <key-property name="TvUnitID" column="TV_UNIT_ID" type="Int32" />
          <key-property name="ActiveFrom" column="ACTIVE_FROM" type="DateTime"/>      
      </composite-id>
      <many-to-one name="TvDevice1"  class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
         <column name="HOUSE_ID"" />
         <column name="TV_DEVICE_ID_1"/>
      </many-to-one>
      <many-to-one name="TvDevice2"  class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
         <column name="HOUSE_ID"" />
         <column name="TV_DEVICE_ID_2"/>
      <many-to-one name="TvDevice3"  class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
         <column name="HOUSE_ID"" />
         <column name="TV_DEVICE_ID_3"/>
      <property .....
   </class>
</hibernate-mapping>


Problem is in many-to-one association of TvDevice1,2,3 in TvUnit class- these columns may contain NULLs and in that case i need the references to be null. But thats is the problem because first part of foreign key (HOUSE_ID) is never NULL (it is part of primary key) so when NHibernate loads entity TvUnit with some of TV_DEVICE_ID_x columns null, it then tries to load TvDevice entity using (ID_HOUSE,0) key to TV_DEVICE table (it uses 0 instead of null for second part of key) - and then throws excetion that object cannot be located (of cource)....

Any thoughts ?
Thanks!
Michal
Hibernate version: 1.0.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 3:10 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 10:48 am
Posts: 33
Hi, try to add not-null atribute "false" for each column.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 2:14 am 
Newbie

Joined: Thu Jun 22, 2006 4:04 am
Posts: 8
Location: Prague
it was first thing i was tring but it didn't help...... From what i know, not-null is used only for schema generation, isn't it ?

Any other ideas ?

Liwoj


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.