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.  [ 5 posts ] 
Author Message
 Post subject: Self-join mapping problem
PostPosted: Thu Jul 19, 2007 4:14 am 
Newbie

Joined: Tue Jul 17, 2007 6:44 am
Posts: 5
Hi everybody again.

Yet another question: I want to bind self-joined table. I have following semantics:

Code:
class Class1
{
    ...
   public virtual IList<Class1> SubjectedClasses
   {
      {getter;setter;}
   }   
   public virtual Class1 OwnerClass
   {
      {getter;setter;}
   }
}


As you see, it is one-to-many semantics (each class may have one owner and may have many subjected classes). So I have the next mapping code:

Code:
<class name="Class1" table="ClassConfiguration" lazy="false">
<id name="ClassId" column="ClassId">
   <generator class="native" />
</id>
<list name="SubjectedClasses" table="ClassConfiguration" lazy="false">
   <key column="OwnerClass"/>
   <index column="ClassId"/>         
   <one-to-many class="Class1"/>
</list>


SQL2005 table:
Code:
[ClassId] [int] IDENTITY(1,1) NOT NULL,
[OwnerClass] [int] NULL


The problem is so far: when I try to save instance of Class1, I get records with OwnerClass == NULL, even if this class is really owned by some other (i.e. property ClassOwner != null). I tried to check if this instance is owned and save owner beforehand, no way. Can you tell me source of this problem?

Thaks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
What you have here, essentially, is a bidirectional one-to-many association regardless of the fact that it is a self join. As such, all rules and conventions that surround a bidirectional one-to-many association apply. One thing that I spotted is that the mapping of the SubjectedClasses property should be inversed (i.e. inverse="true"). Therefore, it will not be enough to just put objects into the SubjectedClasses; their OwnerClass will not be automatically set. In other words, you need to somehow set the OwnerClass property yourself before saving.

Hope this helps.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 11:53 pm 
Newbie

Joined: Tue Jul 17, 2007 6:44 am
Posts: 5
karlchu wrote:
What you have here, essentially, is a bidirectional one-to-many association regardless of the fact that it is a self join. As such, all rules and conventions that surround a bidirectional one-to-many association apply. One thing that I spotted is that the mapping of the SubjectedClasses property should be inversed (i.e. inverse="true"). Therefore, it will not be enough to just put objects into the SubjectedClasses; their OwnerClass will not be automatically set. In other words, you need to somehow set the OwnerClass property yourself before saving.

Hope this helps.


Thanks Karl, but none of your advices help. I tried to add inverse="true". I tried to set OwnerClass manually ever before. The problem is the same: in the database ClassOwner is still NULL.

I have the following code when I try to save my objects now:
Code:
using (ISession nsess = _factory.OpenSession())
{       
                foreach (Class1 cfg in _session.SessionCfg)
                    {
                        if (cfg.OwnerClass != null)
                            nsess.SaveOrUpdate(ccfg.OwnerClass);
                        nsess.SaveOrUpdate(ccfg);
                    }       
}


Mapping now is as follows:
Code:
<class name="Class1" table="ClassConfiguration" lazy="false">
<id name="ClassId" column="ClassId">
         <generator class="native" />
      </id>
      <list name="SubjectedClasses" table="ClassConfiguration" lazy="false" cascade ="all" inverse="true">
         <key column="OwnerClassConfiguration"/>
         <index column="ClassConfigurationId"/>      
         <one-to-many class="ClassConfiguration"/>
      </list>
</class>


May the problem be in wrong mapping of key and index? Or what direction I shall try to dig?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 12:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
My bad. In your original post, the class is set up as a bidirectional association, but the mapping was set up as a unidirectional one-to-many association. Therefore, it was presumptuous on my part that you were intending to create a bidirectional association.

At any rate, if a bidirectional association is really what you intended, you also need to map the OwnerClass property

Code:
<class name="Class1" table="ClassConfiguration" lazy="false">
      <id name="ClassId" column="ClassId">
         <generator class="native" />
      </id>
      <many-to-one name="OwnerClass" column="OwnerClass"/>
      <list name="SubjectedClasses" table="ClassConfiguration" lazy="false" cascade ="all" inverse="true">
         <key column="OwnerClassConfiguration"/>
         <index column="ClassConfigurationId"/>     
         <one-to-many class="ClassConfiguration"/>
      </list>
</class>


One other thing that is confusing to me: do you have a "ClassConfiguration" class? It seems to me the <one-to-many> element should look something like this:
Code:
         <one-to-many class="Class1"/>


If none of these help, I would suggest removing the IList and focus on getting a unidirectional many-to-one association to work first. Then try to add back the IList and get the other direction to work.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 12:49 am 
Newbie

Joined: Tue Jul 17, 2007 6:44 am
Posts: 5
That's worked! Thanks Karl, I was really inattentive when I read your post about bi-directional stuff. After I added <many-to-one name="OwnerClass" column="OwnerClass"/> all works fine.


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.