Thanks Anne for your response. 1) There is no bad data in the discriminator column. I am running this over a test database, and I have only 1 row in the UserRoles table.
2) I tried with an interface. But same error. I don't think its the abstract class causing this issue. The error message says - "Cannot instantiate abstract class or interface". Could it be the composite id? <composite-id> <key-many-to-one name="AssignedTo" class="User" column="UserID" /> <key-many-to-one name="Type" class="RoleType" column="RoleID" /> </composite-id> Thanks, Rohit On May 4, 8:55 pm, Anne Epstein <[email protected]> wrote: > Hi Rohit, welcome to the group! Looking at your issue, the error > message is about an abstract class, so my guess you're having problems > because it's trying to work with Roles directly. > > Thinking about this a little more, here's a thought: what will happen > if you have some bad data in your discriminator column-say, null, or > something other than 1 or 2. It'll try to create a concrete Role class > instead of your subclasses. That would certainly generate your error, > or something very similar. > > Alternatively, you may have better luck if you instead used an > interface, as illustrated > here:http://www.nhforge.org/doc/nh/en/index.html#inheritance-tableperclass > if you're still having problems, it's worth a try...even if the > classes inherit from a class behind the scenes, I think it's safer to > work with an interface they share. > > On Mon, May 4, 2009 at 6:36 PM, Rohit <[email protected]> wrote: > > > Hi, > > > I am using NHibernate version 2.0.1. > > > I have an User class which has a collection of 'Role'. Role is an > > abstract class from which 2 concrete classes derive - 'Mentor' and > > 'Teacher'. > > > _______ ___________ > > | | | | > > | User | ------------------>| Role | > > |______| | __________| > > ^ > > | > > ________ |__________________ > > _______ |___ _________ |__________ > > | | > > | | > > | Mentor | | > > Teacher | > > |___________| |____________________| > > > The User mapping is as follows: > > > <class name="User" table="User"> > > <id name="Id" column="UserID"> > > <generator class="native" /> > > </id> > > <set name="Roles"> > > <key column="UserID" /> > > <one-to-many class="Role" /> > > </set> > > </class> > > > And the Role mapping is as: > > > <class name="Role" table="UserRoles"> > > <composite-id> > > <key-many-to-one name="AssignedTo" class="User" column="UserID" / > > > <key-many-to-one name="Type" class="RoleType" column="RoleID" /> > > </composite-id> > > > <discriminator column="RoleID" insert="false" /> > > > <subclass name="Mentor" discriminator-value="1" /> > > <subclass name="Teacher" discriminator-value="2" /> > > </class> > > > As we can see, Role has a composite id, which is a mapping to the User > > class and a RoleType class. > > > When I try to retrieve a role for a user, I am getting the following > > error: > > NHibernate.InstantiationException : Cannot instantiate abstract class > > or interface:Role > > > The subclass has always worked for me with other objects. Only here, > > when using a composite-id, I am getting this error. > > > Can somebody help me please? This is the first time I am posting on > > this forum. So if there is any additional information I can provide, > > please let me know. > > > Many Thanks, > > Rohit --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
