-->
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.  [ 7 posts ] 
Author Message
 Post subject: CProxyType and inherit classes causes 'Cast' problem
PostPosted: Thu Mar 29, 2007 9:31 am 
Newbie

Joined: Thu Mar 29, 2007 8:11 am
Posts: 6
Hello,

I get the following exception:

Consumer consumer = GetDaoConsumer (ID);

if (consumer.Type == EnumConsumer.Person)
Person person = (Person) consumer;

Cannot cast 'consumer' (which has an actual type of 'CProxyTypeMyNamespaceConsumerMyAssembly_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2') to 'MyNamespace.Person'


Person, NonPerson are joined SubClasses of Consumer in the hbm.xml file

Person, NonPerson are also normal C# Classes based on the class Consumer

Person, NonPerson and Consumer are only connected via the hm.xml file with NHibernate

Is there any way to access the properties of a Person or NonPerson object?

The only thing that helps is to set lazy="false" for all 3 class definitions in the hbm.xml file. Then there are no proxy classes used...


Anyone out who can help me or has an idea?


Best regards
Pearli

NHibernate 1.2 und .NET 2.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 2:50 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
The proxies generated by NHibernate are sublcasses of your underlying objects, so this cast will not work. You either need to change your design or avoid lazy-loading in this case.

MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 3:18 am 
Newbie

Joined: Thu Mar 29, 2007 8:11 am
Posts: 6
Thanks Mike for the answer.

I have guess that, but I've hoped it is not that way...

Regards
Rolf


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 3:21 am 
Newbie

Joined: Thu Mar 29, 2007 8:11 am
Posts: 6
Hey Mike,

do you know it this bevavior was also in NHibernate 1.04 (lazy was false on default) or is it since 1.2 (lazy is true on default)?

Because we have this problem since we upgraded to Nhibernate 1.2.

Regards
Rolf


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 3:48 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This is related to laziness. If you would have set lazy="true" for a class in NHibernate 1.0.4, you would see the same behavior as well.

And it's documented, see http://www.hibernate.org/hib_docs/nhibernate/html/performance.html#performance-proxies.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:29 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
This is in Hibernate in Action too on page 235...

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:41 am 
Newbie

Joined: Thu Mar 29, 2007 8:11 am
Posts: 6
Thanks guy's

for the quick answers...

Best regards
Rolf


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