-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Association with a polymorphic collection problem
PostPosted: Fri Feb 05, 2010 7:25 am 
Newbie

Joined: Tue Dec 22, 2009 8:38 am
Posts: 18
Hello everyone,

I have the following classes:
Code:
public class A
{
    private int id;
    private string description;

    private C nameC;

    public int Id
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }
    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }
    public C NameC
    {
        get
        {
            return nameC;
        }
        set
        {
            nameC = value;
        }
    }
}

public class B:A
{}

public class C
{
    private int id;
    private string name;

    public int Id
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}


Class B inherits class A, class C is associated to class B.
The above classes are mapped to the tables A(Id, Description, NameC), B(Id), C(Id, Name).

There is a many-to-one unidirectional association from class A to class C, and class B is mapped as a joined subclass of class A.

I am trying to retrieve a list of C instances (List<C>) using the following function:
Code:
       public IList LoadAll(Type t)
       {
           ISession session = this.OpenSession();
           try
           {
               BeginTransaction();
               ICriteria criteria = session.CreateCriteria(t);
               if (autoCommit)
                   CommitTransaction();

               return criteria.List();
           }
           catch (HibernateException e)
           {
               throw new PersistenceException(String.Format("{0}", "Failed to load!"), e);
           }
           finally
           {
               if (autoCloseSession)
                   CloseSession();
           }

, where Type t is C.

What I am receiving is a collection of class A instances. Within a instance of A, I have the properties Id, Description and the object NameC. The property Id and Description recibe the corresponding values from the Db. The object NameC have its properties Name with value, but the property Id has the value 0.

Why the property Id of the object NameC doesn't receive the value from the Db. The object C is accessed via its proxy, so it seems that the problem might be there.
I have browsed for a solution for this kind of problem but I did't manage to clarify it.

Has anyone faced this problem and how did you manage to solve it ?

Thank you,
C

------------------------------------
I believe that the behaviour above mentioned is caused by the fact that the A.NameC attribute is not being proxied / or the proxy is not initialized. That was strange, as I mapped all the classes with the Lazy="true" attribute. However I have managed to read correctly the A.NameC property, by reading its proxy:

Code:
var proxy = A.NameC as INHibernateProxy;
C correctC = (c)proxy.HibernateLazyInitializer.GetImplementation();


Anyway, does anyone know why the proxy has not been initialized ? Have I missed anything ?

Thank you,
C.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.