NHibernate version: 1.2.0
Hi,
I need help with this problem.
I wrote some classes demonstrate the problem. Associations between classes are PClass1 -> PClass1 and PClass1 -> PClass2. Test code:
Code:
namespace WindowsApplication11
{
public interface IPClass1
{
int Id { get; set; }
string Attr1 { get; set; }
PClass1 Parent { get; set; }
PClass2 AssociationToPClass2 { get; set; }
}
[NHibernate.Mapping.Attributes.Class(0, Name = "WindowsApplication11.PClass1, WindowsApplication11", Proxy = "WindowsApplication11.IPClass1, WindowsApplication11", Table = "Tab1")]
public class PClass1
{
[NHibernate.Mapping.Attributes.Id(0, Name = "Id", Column = "Id", TypeType = typeof(Int32), UnsavedValue = "-1")]
[NHibernate.Mapping.Attributes.Generator(1, Class = "increment")]
private int _Id = -1;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
[NHibernate.Mapping.Attributes.Property(0, Name = "Attr1", Column = "Attr1", NotNull = false, Length = 50, Access = "field.pascalcase-underscore")]
private string _Attr1 = null;
public string Attr1
{
get { /* any code */ return _Attr1; }
set { /* any code */ }
}
[NHibernate.Mapping.Attributes.ManyToOne(0, Name = "Parent", Class = "WindowsApplication11.PClass1, WindowsApplication11", Column = "IdParent", ForeignKey = "FK_Parent", NotNull = false, Fetch = NHibernate.Mapping.Attributes.FetchMode.Join, Access = "field.pascalcase-underscore")]
private PClass1 _Parent = null;
public PClass1 Parent
{
get { /* any code */ return _Parent; }
set { /* any code */ }
}
[NHibernate.Mapping.Attributes.ManyToOne(0, Name = "AssociationToPClass2", Class = "WindowsApplication11.PClass2, WindowsApplication11", Column = "IdAssociationToPClass2", ForeignKey = "FK_AssociationToPClass2", NotNull = false, Fetch = NHibernate.Mapping.Attributes.FetchMode.Join, Access = "field.pascalcase-underscore")]
private PClass2 _AssociationToPClass2 = null;
public PClass2 AssociationToPClass2
{
get { /* any code */ return _AssociationToPClass2; }
set { /* any code */ }
}
public PClass1()
{
/* any code */
}
/* any other methods and properties */
}
public interface IPClass2
{
int Id { get; set; }
string Attr1 { get; set; }
}
[NHibernate.Mapping.Attributes.Class(0, Name = "WindowsApplication11.PClass2, WindowsApplication11", Proxy = "WindowsApplication11.IPClass2, WindowsApplication11", Table = "Tab2")]
public class PClass2
{
[NHibernate.Mapping.Attributes.Id(0, Name = "Id", Column = "Id", TypeType = typeof(Int32), UnsavedValue = "-1")]
[NHibernate.Mapping.Attributes.Generator(1, Class = "increment")]
private int _Id = -1;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
[NHibernate.Mapping.Attributes.Property(0, Name = "Attr1", Column = "Attr1", NotNull = false, Length = 50, Access = "field.pascalcase-underscore")]
private string _Attr1 = null;
public string Attr1
{
get { /* any code */ return _Attr1; }
set { /* any code */ }
}
public PClass2()
{
/* any code */
}
/* any other methods and properties */
}
}
In my database is:
Tab2 (PClass2)
Id, Attr1
1 , "xxx"
Tab1 (PClass1)
Id, Attr1, IdParent, IdAssociationToPClass2
1 , "xxx", NULL , 1
If I load PClass1 with id=1 the function Get throw this exception "Unable to cast object of type 'ProxyInterfaceSystemSystemObject_WindowsApplication11IPClass2_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable' to type 'WindowsApplication11.PClass2'." This code demonstrate reading of PClass1:
Code:
ISession s = _factory.OpenSession();
ITransaction t = s.BeginTransaction();
int id = 1;
PClass1 p1 = s.Get<PClass1>(id);
t.Commit();
s.Close();
But if I change order of associations properties (Parent, AssociationToPClass2) it work perfect. In any more complex cases is not possible to change property order (e.g. if one property is in superclass and second is in subclass). Furthermore I use generated code where I need no depending on property order.