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.  [ 4 posts ] 
Author Message
 Post subject: Cannot Cast Superclass (Interface) to a Subclass (Concrete)
PostPosted: Tue Sep 26, 2006 5:57 pm 
Newbie

Joined: Thu Sep 14, 2006 8:52 pm
Posts: 12
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
1.0.2 and 1.2.0.Beta1
I have spend a lot time to trying to figure this out and it is important to get this resolved before I can recommend Nhibernate on our project so Thx much for your help.

I have two concrete sub-classess that implement an Interface.

Both the sub-classess have identical attributes, however they have different associations.

One of the concrete subclass let's say ClassA has a many-to-many association with another class, let's say ClassB.

Therefore ClassB has a property, which is a collection of ClassA. After I reconstitute ClassB from the database and access its property (collection of ClassA objects); What I get back is a collection of nhibernate proxies that can only be cast to the super class Interface.

Nhibernate throws an exception when I try to cast an proxy to ClassA. I need to cast it to ClassA, because ClassA has associations which is not shared by the Interface or other sub-class ClassB.

The exception message I get is -
Unable to cast object of type 'ProxyInterfaceSystemObject_IPerson_INHibernateProxy_ISerializable' to type 'NHibernateQuickStartExample.Professor'.

BTW I have run into this problem while implementing Justin Gehtland's tutorial - http://www.theserverside.net/tt/article ... NHibernate

For those of you who have implemented this tutorial, restating the problem in terms of the objects in the tutorial - I retrieve "Department" from the database. I access its property that represents a collection of "Professors". However, I can only cast each object in the Professors collection as a "Person" which is an Interface.

I communicated this issue with Justin, he thinks maybe the libraries may have changed since he wrote the article in 2004.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 7:07 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
The quick and dirty solution:

Code:
List<Professor> allProfs = new List<Professor>();
foreach (IPerson person in department.Professors)
{
   Professor prof = session.Get(typeof(Professor), person.personid) as Professor;
   if (null != prof) allProfs.Add(prof);
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 7:10 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
Maybe I'm missing something, but the other solution seems to be to declare Department.Professors as a collection of Professors, not as a collection of Person as it is now.

Finally, I'd say the domain model is broken. What happens if a Professor decides to take a class? How does the model in the example allow someone to be both a Student and a Professor? It doesn't. Of course, it can be forgiven because it's just an example. But for a real system, you would want Professor, Staff, Student, etc. to be a role, not a class.

Inheritence is not the answer to every modelling problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 8:16 pm 
Newbie

Joined: Thu Sep 14, 2006 8:52 pm
Posts: 12
rponton,

I thought I may have to explicitly retrieve each individual Professor as you did in the first response. I take it when NHibernate retrieves the Department.Professors it returns a collection of proxies and the proxies can only be cast to an Interface.

But your second response suggests I could directly get back Professors as a collection of Professor's or maybe I am not following, Can you please elaborate your comment - "the other solution seems to be to declare Department.Professors as a collection of Professors, not as a collection of Person as it is now".

Isn't it true in NHibernate you can only declare collections as one of the collection interfaces such as bag, set, etc so I cannot possible declare Department.Professors as a collection of Professors.

btw Your first response works fine, Thx much for your help.


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