-->
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.  [ 3 posts ] 
Author Message
 Post subject: hbm.xml for the class Cat has a cats:List<Cat> field.
PostPosted: Tue Jun 10, 2008 3:56 am 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
Class Cat has a cats field declared as List<Cat>. How should I write configuration file for loading cats field?

I tried the following, but does not work. cats field is null after loading.

Code:
   <class name="Cat" table="cat" lazy="true">
      <id name="catId" column="cat_id"
         type="long">
         <generator class="sequence">
            <param name="sequence">sq_cat</param>
         </generator>
      </id>
      <property name="name" length="100" column="name" />
      <bag name="cats">
         <key column="cat_id" />
         <one-to-many
            class="my.Cat" />
      </bag>
   </class>


Thanks

superZZ
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 5:35 am 
Newbie

Joined: Tue May 20, 2008 8:22 am
Posts: 2
Hi,
there are four missed points in your configuration
1- i think you cannot use same primary key as foreign key in your scenario
2- bag should have cascade attribute with value save-update or all-delete-orphan
3- you have to add many-to-one relation to the parent "cat"
4- its recommended in such case to set true to attribute inverse

configuration file will be
Code:
    <class name="my.Cat" table="cat" lazy="true">
      <id name="catId" column="cat_id"
         type="long">
         <generator class="hilo">
            <param name="max_lo">100</param>
        </generator>
      </id>

      <property name="name" length="100" column="name" />


      <bag name="cats" inverse="true" cascade="all-delete-orphan" >
         <key column="parent_cat_id" />
         <one-to-many
            class="my.Cat" />
      </bag>
     
      <many-to-one name="parentCat" class="my.Cat"
        column="parent_cat_id" />
   </class>


don't forget to add get/set of parentCat and set the parent before save
for example:

Code:
      Session session = sessions.openSession();
      Transaction tx = session.beginTransaction();
      Cat c = new Cat();
      Collection<Cat> cats = new ArrayList<Cat>(5);
      for (int i = 0; i < 5; i++) {
         Cat c1 = new Cat();
                         //set the parent CAT
         c1.setParentCat(c);
         cats.add(c1);
      }
      c.setCats(cats);
      Serializable obj = session.save(c);
      System.out.println(obj);
      tx.commit();
      session.close();




finally, it's better in Cat class to use Collection than List and in this scenario it is better to use set instance of bag

Mahmoud


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:42 pm 
Newbie

Joined: Tue Jun 10, 2008 3:51 am
Posts: 10
Thank you for your reply!

But I think I do need more help here because both of DDL and entity are out of my control.
1. The entity does not have a PARENT field, it only has child field declared as children:List<Cat>.
2. There is no parent_cat_id in Cat table. Instead, database has an additional relation table as
cat_relation:
parent_cat_id:DECIMAL
child_cat_id:DECIMAL

It seems that I need add a sql-query, but I try a lot and never succeed =(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.