Proxy

Dmitry's Avatar

Dmitry

04 May, 2010 08:56 AM via web

Hello!
Does someone use "Proxy" attribute in his mapping? The relationship mapping (using proxy) is especially interesting.

I try to do something like this, but it doesn't work well:

Objects:

public interface ITestObject
{
    Int64 ID { get; set; }
    IList<TestObjectStr> TestObjectStrs { get; set; }
}
public class TestObject : ITestObject
{
    public Int64 ID { get; set; }
    public IList<TestObjectStr> TestObjectStrs { get; set; }
    public TestObject()
    {
        TestObjectStrs = new List<TestObjectStr>();
    }
}

public interface ITestObjectStr
{
    Int64 ID { get; set; }
    TestObject TestObject { get; set; }
}
public class TestObjectStr : ITestObjectStr
{
    public Int64 ID { get; set; }
    public TestObject TestObject { get; set; }
}

Mapping:

public class TestObjectMap : ClassMap<TestObject>
{
    public TestObjectMap()
    {
        Table("TEST");
        Proxy<ITestObject>();
        Id(x => x.ID).Column("TEST_ID")
            .GeneratedBy.Identity()
            .Not.Nullable();
        HasMany(x => x.TestObjectStrs)
            .KeyColumn("TEST_ID")
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

public class TestObjectStrMap : ClassMap<TestObjectStr>
{
    public TestObjectStrMap()
    {
        Table("TEST_STR");
        Proxy<ITestObjectStr>();
        Id(x => x.ID).Column("TEST_STR_ID")
            .GeneratedBy.Identity()
            .Not.Nullable();
        References(x => x.TestObject).Column("TEST_ID")
            .Not.Nullable();
    }
}

Then I do:

IList<TestObjectStr> _objectStrs = session.CreateCriteria<TestObjectStr>().List<TestObjectStr>();

The exception is generated:

NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of TestObjectStr --->  System.InvalidCastException: Cannot cast  an object "INHibernateProxyProxy0d818cae2f81453d98a7fa8e7aca99f7" to "TestObject"

if I change deffinition of TestObjectStr:

public interface ITestObjectStr
{
    Int64 ID { get; set; }
    ITestObject TestObject { get; set; }
}
public class TestObjectStr : ITestObjectStr
{
    public Int64 ID { get; set; }
    public ITestObject TestObject { get; set; }
}

It says that TestObjectStr refers to an unmapped class.

Working sample for this theme will be very much appreciated!

Thanks!

  1. 2 Posted by Dmitry on 04 May, 2010 08:59 AM

    Dmitry's Avatar

    I want to give a more precise definition about objects query:

    IList _objectStrs = session.CreateCriteria().List();

  2. 3 Posted by Dmitry on 04 May, 2010 09:02 AM

    Dmitry's Avatar

    IList _objectStrs = session.CreateCriteria().List();

  3. 4 Posted by Dmitry on 04 May, 2010 09:05 AM

    Dmitry's Avatar

    I don't know why, but the code concerning CreateCriteria() is modified by site engine. The types of objects are deleted. - needed everywhere

  4. Support Staff 5 Posted by James Gregory on 04 May, 2010 09:24 AM

    James Gregory's Avatar

    Corrected your question. Just needed to indent the exception message.

    One of us will get you an answer to your question soon.

  5. 6 Posted by Dmitry on 07 May, 2010 05:02 AM

    Dmitry's Avatar

    Thanks, the problem is already solved.

  6. Support Staff 7 Posted by James Gregory on 07 May, 2010 07:59 AM

    James Gregory's Avatar

    How did you solve it?

  7. 8 Posted by Dmitry on 12 May, 2010 12:00 PM

    Dmitry's Avatar

    Using Class attribute:

    References(x => x.TestObject).Column("TEST_ID")

            .Class(typeof(TestObject))
            .Not.Nullable();
    
  8. James Gregory closed this discussion on 16 May, 2010 05:02 PM.

Comments are currently closed for this discussion. You can start a new one.