If you use Load for the correct subclass of the same objectit will return the correct proxy. From the documentation (in section 15.1.3 of the 1.2.0 docs):
<snip>
There are some gotchas to be aware of when extending this approach to polymorphic classes, eg.
Code:
<class name="Cat" proxy="Cat">
......
<subclass name="DomesticCat">
.....
</subclass>
</class>
Firstly, instances of Cat will never be castable to DomesticCat, even if the underlying instance is an instance of DomesticCat:
Code:
Cat cat = (Cat) session.Load(typeof(Cat), id); // instantiate a proxy (does not hit the db)
if ( cat.IsDomesticCat ) { // hit the db to initialize the proxy
DomesticCat dc = (DomesticCat) cat; // Error!
....
}
Secondly, it is possible to break proxy ==.
Code:
Cat cat = (Cat) session.Load(typeof(Cat), id); // instantiate a Cat proxy
DomesticCat dc =
(DomesticCat) session.Load(typeof(DomesticCat), id); // acquire new DomesticCat proxy!
System.out.println(cat==dc); // false
However, the situation is not quite as bad as it looks. Even though we now have two references to different proxy objects, the underlying instance will still be the same object:
Code:
cat.Weight = 11.0; // hit the db to initialize the proxy
Console.WriteLine( dc.Weight ); // 11.0
</snip>
The key part is:
DomesticCat dc =
(DomesticCat) session.Load(typeof(DomesticCat), id); // acquire new DomesticCat proxy!
Note that this will not actually hit the DB again - it will just create a new proxy for the underlying object that's already in the session. Using this will break == but if that's not a problem at least it solves your other problem! :)
Cheers,
Symon.