Hi,

Recently we started using a "fine-grained" domain object which
naturally includes lots of polymorphic classes. This has been a
success so far.

However, I encountered a strange "feature" which IMO limits the
usability of mapped class hierarchies: Polymorphic lazy-loaded many-to-
one relations are not initialized with their correct type. The worst
part is that this only happens in certain situations, depending on the
loading mechanism of the proxy.

Consider a class "Car" which has a mapped property "Engine" of type
"AbstractEngine". The "AbstractEngine" class has two derived concrete
classes - "HybridEngine" and "DieselEngine".

When I access the car.Engine property I expect its type to be one of
the concrete types derived from AbstractEngine. Specifically, I expect
to be able to check its type, downcast it to one of the concrete types
and call the properties and methods of the concrete type.

This actually works - but only in certain scenarios: Specifically if
the property is NOT lazy loaded. Say I
access the c.Engine property after getting it from the following
query: "select c from Car join fetch c.Engine" - it works.

But it doesn't work when the proxy is lazy-loaded: "select c from
Car"! What you get is a c.Engine proxy instance that is derived from
AbstractEngine and cannot be checked for type or down-cast to a
concrete type!

This has been discussed in the JIRA before but the issue was closed:

http://forum.hibernate.org/viewtopic.php?t=959834
http://forum.hibernate.org/viewtopic.php?p=2385856&sid=b9e517b5835ffe64da235fd1cdff5d90

One proposed solution was to use NHibernateUtil.GetClass(c.Engine) to
check the "real" type of the proxy. But this does not solve the
downcast problem. Another solution is to use session.Get(ConcreteType,
id).

But I'd like my classes to remain POCOs, ignorant of NHibernate and
the persistence mechanism. I managed to do so until now. But lines
like this:
var engine = session.Get(NHibernateUtil.GetClass(car.Engine),
car.Engine.Id);
Do not work well in this environment.

I'd like the developers to consider this inconsistent behavior as a
bug. I have a failing test for trunk, if anyone wants to take it on.

Thanks,
Roy.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to