-->
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: Inheritance table per class hierarchy
PostPosted: Thu Mar 22, 2007 6:14 pm 
Newbie

Joined: Sun Sep 10, 2006 12:27 pm
Posts: 13
Hi, i have a very strange problem:

I have 2 class (AlertS and AlertP) inherit from a base class:
Code:
  <class name="Alert" table="Alert">
    <id name="ID" column="Id">
      <generator class="native" />
    </id>
    <discriminator column="Type" type="string"></discriminator>
    <subclass name="AlertS" discriminator-value="S">
    </subclass>
    <subclass name="AlertP" discriminator-value="P">
    </subclass>
  </class>


Then i have a class BContract inherit from Contract (table per class Hierarchy) another:
Code:
  <class name="Contract" table="Contracts">
    <id name="ID" column="Id">
      <generator class="native" />
    </id>
    <discriminator column="TypeC" type="string"></discriminator>
    <set name="AlertSc" lazy="true" table="Alert" generic="true" inverse="true" cascade="all-delete-orphan">
      <key column="Code" />
      <one-to-many class="AlertS" />
    </set>

    <subclass name="BContract" discriminator-value="ASS">
      <set name="AlertPr" lazy="true" table="Alert" generic="true" inverse="true" cascade="all-delete-orphan">
        <key column="Code" />
        <one-to-many class="AlertP" />
      </set>
    </subclass>
  </class>


Contract have a collection of AlertS and BContract a collection of AlertP, when i try to load the second collection i obtain an exception: Nhibernate.WrongClassException: "Failed to lazily initialize a collection" -
{"Object with id: 1 was not of the specified sublcass: Test.AlertP (loading object was of wrong class [Test.AlertS])"}
The sql statement, which nhibernate use to load Alert collection object do not use the discriminator value!

Thank for any suggestion


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 22, 2007 7:52 pm 
Newbie

Joined: Sun Sep 10, 2006 12:27 pm
Posts: 13
I have found a workaround, but i do not like it: use the where attribute.

So the mapping:
Code:
  <class name="Contract" table="Contracts">
    <id name="ID" column="Id">
      <generator class="native" />
    </id>
    <discriminator column="TypeC" type="string"></discriminator>
    <set name="AlertSc" where="Type='S'" lazy="true" table="Alert" generic="true" inverse="true" cascade="all-delete-orphan">
      <key column="Code" />
      <one-to-many class="AlertS" />
    </set>

    <subclass name="BContract" discriminator-value="ASS">
      <set name="AlertPr" where="Type='P'" lazy="true" table="Alert" generic="true" inverse="true" cascade="all-delete-orphan">
        <key column="Code" />
        <one-to-many class="AlertP" />
      </set>
    </subclass>
  </class>


Another solution?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 5:37 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i am pretty sure your "work-around" is the correct solution for this type of problem. you have to tell NH which class-type you want and, using table per class hierarchy like you are, you have to tell NH which discriminator.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 1:27 pm 
Newbie

Joined: Wed Feb 20, 2008 3:03 pm
Posts: 5
This might not be considered a workaround in NHibernate, but was certainly not necessary in the java version. NHibernate could figure this out based purely on the Type and discriminator-value... so why make us specify the obvious "where" attribute?


Top
 Profile  
 
 Post subject: Re: Inheritance table per class hierarchy
PostPosted: Tue Aug 04, 2009 10:36 am 
Newbie

Joined: Tue Aug 04, 2009 10:21 am
Posts: 1
Is this behaviour considered as a bug?
Is there another workaround but using a where clause with correct discriminator value when mapping associations to subclasses?

I have a very simple scenario, using NHibernate:
- one abstract base class "S"
- two concrete subclasses "A" and "B"
- table per class hierarchy -strategy for the above

I have mapped a class "X" with one-to-many association to both conrete subclasses "A" and "B".
When traversing one of the strongly typed collections, say, of type A (ISet<A>), a WrongClassException is thrown. It seems that collection contains instances of both subclasses "A" and "B"

See also
http://groups.google.com/group/nhusers/browse_thread/thread/00b3e25555035481
https://forum.hibernate.org/viewtopic.php?f=25&t=971271


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.