Proxy exception for multiple joined-subclass
Description
Environment
None
is related to
Activity
Show:
Y February 11, 2015 at 9:13 PM
added pull request to review unit test that reproduces error: https://github.com/nhibernate/nhibernate-core/pull/397
Fixed
Details
Details
Assignee
Alex Zaytsev
Alex ZaytsevReporter
Y
YLabels
Components
Fix versions
Affects versions
Priority
Who's Looking?
Open Who's Looking?
Created February 11, 2015 at 4:34 PM
Updated December 13, 2017 at 10:22 PM
Resolved April 27, 2017 at 3:42 AM
Who's Looking?
I recently upgraded from Nhibernate 3.2 to Nhibernate 4.0, In my previous configuration, my proxy factory class is NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle (v3.1.0.4000). With this setup, the exception below does happen, It's able to cast the proxy instance of Shape to Circle when retrieving ShapeContainer.Shape. After the upgrade, i change my proxy factory class to NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate and the exception below is thrown.
Below is my setup (I will be committing the unit test to the repository later today):
C#
public interface IShape { Guid Id { get; set; } string Property1 { get; set; } } public interface ISquare : IShape { string Property2 { get; set; } string Property3 { get; set; } } public interface ICircle : IShape { string Property2 { get; set; } string Property3 { get; set; } } public interface IShapeContainer { Guid Id { get; set; } string Name { get; set; } IShape Shape { get; set; } } public abstract class Shape : IShape { public virtual Guid Id { get; set; } public string Property1 { get; set; } public abstract string Property2 { get; set; } } public class Square : Shape, ISquare { public override string Property2 { get; set; } public virtual string Property3 { get; set; } } public class Circle : Shape, ICircle { public override string Property2 { get; set; } public virtual string Property3 { get; set; } } public class ShapeContainer : IShapeContainer { public Guid Id { get; set; } public string Name { get; set; } public IShape Shape { get; set; } }
mapping file
<class name="Shape" proxy="IShape"> <id name="Id"> <generator class="guid.comb" /> </id> <property name="Property1" /> </class> <joined-subclass proxy="ISquare" extends="Shape" name="Square"> <key column="ShapeId" /> <property name="Property2"/> <property name="Property3"/> </joined-subclass> <joined-subclass proxy="ICircle" extends="Shape" name="Circle"> <key column="ShapeId" /> <property name="Property2"/> <property name="Property3"/> </joined-subclass> <class name="ShapeContainer" proxy="IShapeContainer"> <id name="Id"> <generator class="guid.comb" /> </id> <property name="Name" /> <many-to-one name="Shape" class="Shape" /> </class>
error
System.Reflection.TargetException : Object does not match target type. at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at NHibernate.Proxy.DefaultLazyInitializer.Intercept(InvocationInfo info) in DefaultLazyInitializer.cs: line 34 at INHibernateProxyProxy.get_Property2()
Unit Test
var result1 = (from e in session.Query<ShapeContainer>() where e.Name == "Circle" select e).ToList(); var result2 = (from e in session.Query<ShapeContainer>() where e.Name == "Square" select e).ToList(); var circle = (ICircle) result1[0].Shape; var square = (ISquare) result2[0].Shape; Assert.IsNotNull(circle.Property1); Assert.IsNotNull(circle.Property2); // error is thrown here Assert.IsNotNull(circle.Property3); Assert.IsNotNull(square.Property1); Assert.IsNotNull(square.Property2); Assert.IsNotNull(square.Property3);