-->
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.  [ 5 posts ] 
Author Message
 Post subject: Two mappings to the same table
PostPosted: Wed Apr 19, 2006 3:53 am 
Newbie

Joined: Wed Apr 19, 2006 3:36 am
Posts: 2
Hello,

I have definded two mapping to the same database table, you can read it later. I do it because I want a Project and another ProjectExtended that have herency from Project and include more bags.

The problem is that when requeste the follow HqlString:

select distinct p from Project as p where p.ProjectNumber LIKE %

I get a IList with the same two times. One to class Project and other to ProjectExtended. By example, If the table has three rows I get six object: three Project and three ProjectExtended.

What is the problem?


Hibernate version:1.0.1

Mapping documents:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="GEP.Exides.Instrumentation.Bussiness.Projects.Model.Project, GEP.Exides.Instrumentation.Bussiness" table="GEPIN_ING_PROJECT">
      <id name="Id" column="ID" type="Decimal" unsaved-value="-1">
         <generator class="sequence">
            <param name="sequence">GEPIN_ING_PROJECT_SEQ</param>
         </generator>
      </id>
      
      <property name="Initiator" column= "INITIATOR" type="AnsiString" length="9"/>
      <property name="ProjectNumber" column="PROJECT_NUMBER" type="AnsiString" length="20" not-null="true"/>
      <property name="Moc" column= "MOC" type="AnsiString" length="20"/>
      <property name="Description" column="DESCRIPTION" type="AnsiString" length="400"/>
      <property name="Notes" column="NOTES" type="AnsiString" length="400"/>
      <property name="Remarks" column="REMARKS" type="AnsiString" length="400"/>

      <bag name="Locations" table="GEPIN_ING_PROJECTLOC" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Projects.Model.ProjectLocation, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>

      <property name="LastUpdateDate" column="LAST_UPDATE_DATE" type="DateTime" not-null="true"/>
      <property name="LastUpdatedBy" column="LAST_UPDATED_BY" type="AnsiString" length="9" not-null="true"/>
      <property name="CreationDate" column="CREATION_DATE" type="DateTime" not-null="true"/>
      <property name="CreatedBy" column="CREATED_BY" type="AnsiString" length="9" not-null="true"/>
      <property name="DeleteMark" column="DELETE_MARK" type="Decimal" not-null="true"/>
   </class>
</hibernate-mapping>




Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="GEP.Exides.Instrumentation.Bussiness.Projects.Model.ProjectExtended, GEP.Exides.Instrumentation.Bussiness" table="GEPIN_ING_PROJECT">
      <id name="Id" column="ID" type="Decimal" unsaved-value="-1">
         <generator class="sequence">
            <param name="sequence">GEPIN_ING_PROJECT_SEQ</param>
         </generator>
      </id>
      
      <property name="Initiator" column= "INITIATOR" type="AnsiString" length="9"/>
      <property name="ProjectNumber" column="PROJECT_NUMBER" type="AnsiString" length="20" not-null="true"/>
      <property name="Moc" column= "MOC" type="AnsiString" length="20"/>
      <property name="Description" column="DESCRIPTION" type="AnsiString" length="400"/>
      <property name="Notes" column="NOTES" type="AnsiString" length="400"/>
      <property name="Remarks" column="REMARKS" type="AnsiString" length="400"/>

      <bag name="Locations" table="GEPIN_ING_PROJECTLOC" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Projects.Model.ProjectLocation, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>

      <bag name="TypicalLoops" table="GEPIN_ING_TYPICALLOOP_ILD" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Typicals.Model.TypicalLoop, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>

      <bag name="InstrumentIndex" table="GEPIN_ING_PROJECTOBJ" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Projects.Model.ProjectInstrumentIndex, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>

      <bag name="Sfdb" table="GEPIN_ING_PROJECTOBJ" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Projects.Model.ProjectSfdb, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>
      
      <bag name="Revisions" table="GEPIN_ING_REVISION_CONNECTION" lazy="false" cascade="all" inverse="true" order-by="ID ASC">
         <key column="PROJECT_ID"/>
         <one-to-many class="GEP.Exides.Instrumentation.Bussiness.Revision.Model.RevisionConnection, GEP.Exides.Instrumentation.Bussiness"/>
      </bag>
      
      <property name="LastUpdateDate" column="LAST_UPDATE_DATE" type="DateTime" not-null="true"/>
      <property name="LastUpdatedBy" column="LAST_UPDATED_BY" type="AnsiString" length="9" not-null="true"/>
      <property name="CreationDate" column="CREATION_DATE" type="DateTime" not-null="true"/>
      <property name="CreatedBy" column="CREATED_BY" type="AnsiString" length="9" not-null="true"/>
      <property name="DeleteMark" column="DELETE_MARK" type="Decimal" not-null="true"/>
   </class>
</hibernate-mapping>


Name and version of the database you are using: Oracle
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 4:31 am 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
You need to put the polymorphism="explicit" attribute in your <class> elements.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 2:06 pm 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
Wouldn't it be preferable to use one mapping file, with ProjectExtended being a subclass of Project?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 8:39 pm 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
That wouldn't work because a record would then need to be either a Project instance or a ProjectExtended instance. I believe the intent here is to be able to decide at load time whether an instance shall be loaded as a Project or ProjectExtended instance depending on whether the additional properties are needed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 11:00 am 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
Ah, the lightweight class pattern. http://www.hibernate.org/41.html
I haven't run into a situation where I needed to do something like this, so it never occurred to me what the mapping requirements might be. Always nice to learn something new.


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