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: one parent-item inheriting to two subclasses
PostPosted: Mon Aug 21, 2006 7:42 am 
Newbie

Joined: Tue Jul 11, 2006 3:26 am
Posts: 6
Problem:
I have a class Company. Inheriting from this class: subclass Broker and subclass Insurer.

I have a company which can be Broker and Insurer, depending on the circumstances. This works, until I have a contract where the Broker is the same company as the Insurer.
Trying to access this contract I get an error (see below)

I understand that Nhibernate internally keeps track of objects and when I load the Broker-object, it also loads the information from the Company-object and 'casts' it to a Broker. When afterwards I try to load the Insurer-object which tries to load the same Company-object, it has a problem 'casting' this company-object (which has been 'cast' to a Broker-object). I do not see a solution so I turn to you friendly people...

Error:
Code:
{"Object with id: 46 was not of the specified sublcass: POS.URS.POS_Insurance_BL.Insurer (loading object was of wrong class)"}


Full stack trace of any exception that occurs:
" at NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs, Int32 i, ILoadable persister, String suffix, Key key, Object obj, LockMode lockMode, ISessionImplementor session)\r\n at NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, String[] suffixes, Key[] keys, Object optionalObject, Key optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session)\r\n at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, IList hydratedObjects, Object optionalObject, Object optionalId, Key[] keys, Boolean returnProxies)\r\n at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)\r\n at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)\r\n at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)\r\n at NHibernate.Loader.CriteriaLoader.List(ISessionImplementor session)\r\n at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria)\r\n at NHibernate.Impl.CriteriaImpl.List()\r\n at POS.URS.POS_Library_BL.NHibFactory`1.GetList(String sortedBy) in C:\\Projects\\POS\\POS_URS\\SOURCE\\POS_Library_BL\\NHibFactory.cs:line 66\r\n

Mapping documents:
Code:
<class
    name='POS.URS.POS_HRM_BL.Company, POS_HRM_BL'
    table='tbl_Company'>
   <id
       name='Id'
       access='field.pascalcase-underscore'
       column='Company_Id'
       type='Int32'>
      <generator
          class='identity' />
   </id>
   <property
       name='Name'
       type='String'
       column='Company_Name' />
   <property
       name='Address'
       type='String'
       column='Company_Address' />
   <property
       name='PostalCode'
       type='String'
       column='Company_Postal_Code' />
   <property
       name='City'
       type='String'
       column='Company_City' />
   <property
       name='Code'
       type='String'
       column='Company_Cd' />
   <property
       name='Telephone'
       type='String'
       column='Company_Telephone_Nmbr' />
   <many-to-one
       name='Country'
       class='POS.URS.POS_HRM_BL.Country, POS_HRM_BL'
       column='Company_Country_Id' />
</class>

<joined-subclass
    name='POS.URS.POS_Insurance_BL.Broker, POS_Insurance_BL'
    extends='POS.URS.POS_HRM_BL.Company, POS_HRM_BL'
    table='Broker'>
   <key
       column='Company' />
   <bag
       name='InsurancePoliciesCompany'
       table='InsurancePolicyCompany'
       lazy='true'
       inverse='true'>
      <key
          column='Broker' />
      <one-to-many
          class='POS.URS.POS_Insurance_BL.InsurancePolicyCompany, POS_Insurance_BL' />
   </bag>
</joined-subclass>

<joined-subclass
    name='POS.URS.POS_Insurance_BL.Insurer, POS_Insurance_BL'
    extends='POS.URS.POS_HRM_BL.Company, POS_HRM_BL'
    table='Insurer'>
   <key
       column='Company' />
   <bag
       name='InsurancePoliciesCompany'
       table='InsurancePolicyCompany'
       lazy='true'
       inverse='true'>
      <key
          column='Insurer' />
      <one-to-many
          class='POS.URS.POS_Insurance_BL.InsurancePolicyCompany, POS_Insurance_BL' />
   </bag>
</joined-subclass>

<class
    name='POS.URS.POS_Insurance_BL.InsurancePolicyCompany, POS_Insurance_BL'
    table='InsurancePolicyCompany'>
   <id
       name='Id'
       access='field.pascalcase-underscore'
       column='InsurancePolicyCompanyId'>
      <generator
          class='identity' />
   </id>
   <many-to-one
       name='InsurancePolicyGroup'
       class='POS.URS.POS_Insurance_BL.InsurancePolicyGroup, POS_Insurance_BL'
       column='InsurancePolicyGroup' />
   <many-to-one
       name='InternalCompany'
       class='POS.URS.POS_HRM_BL.InternalCompany, POS_HRM_BL'
       column='InternalCompany' />
   <many-to-one
       name='Insurer'
       class='POS.URS.POS_Insurance_BL.Insurer, POS_Insurance_BL'
       column='Insurer' />
   <many-to-one
       name='Broker'
       class='POS.URS.POS_Insurance_BL.Broker, POS_Insurance_BL'
       column='Broker' />
   <property
       name='ContractNumber'
       type='String'
       column='ContractNumber' />
   <property
       name='StartDate'
       type='Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate'
       column='InsurancePolicyCompanyStartDate' />
   <property
       name='EndDate'
       type='Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate'
       column='InsurancePolicyCompanyEndDate' />
   <bag
       name='TariffsOfPolicyCompany'
       table='TariffOfPolicyCompany'
       lazy='true'
       inverse='true'>
      <key
          column='InsurancePolicyCompany' />
      <one-to-many
          class='POS.URS.POS_Insurance_BL.TariffOfPolicyCompany, POS_Insurance_BL' />
   </bag>
   <bag
       name='InsurancePoliciesIndividual'
       table='InsurancePolicyIndividual'
       lazy='true'
       inverse='true'>
      <key
          column='InsurancePolicyCompany' />
      <one-to-many
          class='POS.URS.POS_Insurance_BL.InsurancePolicyIndividual, POS_Insurance_BL' />
   </bag>
</class>


Top
 Profile  
 
 Post subject: Re: one parent-item inheriting to two subclasses
PostPosted: Mon Aug 21, 2006 8:13 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
geeken wrote:
Problem:
I have a class Company. Inheriting from this class: subclass Broker and subclass Insurer.


This situation is usually handled not deriving Broker and Insurer from "Company" but from "Role", and adding an association between company and role. I.e. a company can have several roles.

Gert

_________________
If a reply helps You, rate it!


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.