WrongClassException with table per class hierarchy

500 views
Skip to first unread message

Michiel

unread,
Jan 26, 2009, 11:31:59 AM1/26/09
to nhusers
I have a tbale Roles, with columns Id, Name, Description and RoleType.
The last columns determines the class, possible values are
SecurityGroup and Permission.

My mapping looks like this:

<class name="Role" table="Roles">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<discriminator column="RoleType" type="String"/>
<property name="Name" type="String" length="50"/>
<property name="Description" type="String" length="250"/>
<subclass name="Permission" discriminator-value="Permission"/>
<subclass name="SecurityGroup" discriminator-value="SecurityGroup"/
>
</class>

There's an abstract Role class that defines the properties Name and
Description, and classes SecurityGroup and Permission that inherit
from the Role class.

I also have a User class and corresponding table, as well as a many-to-
many mapping between users and roles. For this purpose the User class
has 2 properties of type IList<SecurityGroup> and IList<Permission>.
The mapping for the User class looks like this:

<class name="User" table="Users">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Username" type="string" length="50"/>
<property name="HashedPassword" column="Password" type="string"
length="50"/>
<bag name="SecurityGroups" table="Users_Roles" cascade="all-delete-
orphan" lazy="false">
<key column="UserId"/>
<many-to-many class="SecurityGroup" column="RoleId"/>
</bag>
<bag name="Permissions" table="Users_Roles" cascade="all-delete-
orphan" lazy="false">
<key column="UserId"/>
<many-to-many class="Permission" column="RoleId"/>
</bag>
</class>

When I try session.Get<User>(id) I get the following exception:

XunitException: NHibernate.WrongClassException : Object with id: 1 was
not of the specified subclass: SecurityGroup (loading object was of
wrong class [Permission])

What could be the problem?

Thanks for looking!

Anne Epstein

unread,
Jan 26, 2009, 11:16:34 PM1/26/09
to nhu...@googlegroups.com
Not sure if you already got your answer, but you're going to have to put a where condition on every bag that holds collections made up of subclasses.  It wasn't always that way before NHibernate 1.2, you didn't need the condition, then for some reason that escapes me at the moment, the where condition became required for proper mapping with that version.  Anyway, that should solve your problem.

A

Michiel

unread,
Jan 28, 2009, 4:42:04 AM1/28/09
to nhusers
> Anyway, that should solve your problem.
>

Sure does, thanks!

I did assume that even without the where condition NHibernate would
have enough information to do the right thing. I have found a few
posts on this:

http://forum.hibernate.org/viewtopic.php?p=2345053&sid=b63d628daddc3a6d1545b3582139d101
http://www.mail-archive.com/tapest...@jakarta.apache.org/msg15241.html

There's probably a good reason for it, but it's not very obvious.

-M
Reply all
Reply to author
Forward
0 new messages