Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate: Get real entity class instead of proxied class [duplicate]

Tags:

nhibernate

Is there a way to make nhibernate return my class instead of its proxy class? I dont mind if it's not lazy or cant be updated.

like image 547
ryudice Avatar asked Mar 28 '11 04:03

ryudice


3 Answers

You can unproxy class with this code

session.PersistenceContext.Unproxy(proxiedInstance)
like image 122
Sly Avatar answered Nov 26 '22 22:11

Sly


You should define this in your mapping, by defining lazy="false"

<class name="MyEntity" table="MyTable" lazy="false">
</class>
like image 43
Frederik Gheysels Avatar answered Nov 26 '22 21:11

Frederik Gheysels


You can use following code to get real object

InstanceType instance;
if (proxiedInstance is INHibernateProxy)
{
 var lazyInitialiser = ((INHibernateProxy)proxiedInstance).HibernateLazyInitializer;
 instance = (InstanceType)lazyInitialiser.GetImplementation();
}
like image 39
William Avatar answered Nov 26 '22 22:11

William