-->
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.  [ 9 posts ] 
Author Message
 Post subject: Trivial problem with bidirectional list
PostPosted: Tue Apr 19, 2005 3:31 am 
Newbie

Joined: Tue Apr 19, 2005 2:58 am
Posts: 4
I need some help getting a bidirectional list to work, I probably made a trivial mistake somewhere but I can't figure it out.

The code below works fine when using <set> in the mappings and Set and HashSet instead of List in the java code, however I need to have the children in their order which is not supported by <set>. So I tried to use the <list> tag which is now supported for bidirectional lists in Hibernate3.

The child table contains the "pos" column but all rows contain 0. When reading in, only one child is retrieved, not both.

Please advise what I have to do so that the pos column contains the index of my list.

I have omitted all constructors, getters and setters as they were generated by Eclipse.

Hibernate version: version 3.0 (28.2.2005)

Parent.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="sample">
    <class name="Parent" table="parent">
        <id name="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" type="string"/>
       <list name="children" inverse="true" cascade="all-delete-orphan">
          <key column="parent_id"/>
          <list-index column="pos"/>
          <one-to-many class="Child"/>
       </list>
    </class>
</hibernate-mapping>


Child.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="sample">
    <class name="Child" table="child">
        <id name="id" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" type="string"/>
        <property name="pos" type="integer"/>
        <many-to-one name="parent" column="parent_id" not-null="true"/>
    </class>
</hibernate-mapping>


Class Parent.java:
Code:
package sample;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;


public class Parent {

   private Long   id;
   private String   name;
   private List   children   = new ArrayList();

        // constructors, getters, setters generated by Eclipse,
        // omitted here

   public void addChild(Child child) {

      children.add(child);
      child.setParent(this);
   }

   public void removeChild(Child child) {

      if (children.remove(child))
         child.setParent(null);
   }
}

Class Child.java:
Code:
package sample;


public class Child {

   private Long   id;
   private int      pos;
   private String   name;
   private Parent   parent;

        // constructors, getters, setters generated by Eclipse,
        // omitted here
}

Main:
Code:
package sample;

import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;


public class TestList {

   public static void main(String[] args) {

      Parent parent = new Parent();
      parent.setName("Parent");

      Child child1 = new Child();
      child1.setName("Child 1");
      parent.addChild(child1);
      Child child2 = new Child();
      child2.setName("Child 2");
      parent.addChild(child2);

      for (Iterator iter = parent.getChildren().iterator(); iter.hasNext();) {
         Child child = (Child) iter.next();
         System.out.println(child.getName());
      }

      try {
         Configuration cfg = new Configuration();
         cfg.addClass(Parent.class);
         cfg.addClass(Child.class);
         cfg.setProperty(Environment.HBM2DDL_AUTO, "create");
         cfg.setProperty("hibernate.show_sql", "true");

         SessionFactory sf = cfg.buildSessionFactory();
         Session s = sf.openSession();
         Transaction tx = s.beginTransaction();

         s.save(parent);
         tx.commit();
         s.flush();
         s.close();
         s = null;
         sf.close();
         sf = null;
         cfg = null;

      } catch (Exception e) {
         System.out.println("Exception in Hibernate:" + e.getMessage());
      }

      try {
         Configuration cfg = new Configuration();
         cfg.addClass(Parent.class);
         cfg.addClass(Child.class);
         cfg.setProperty("hibernate.show_sql", "true");

         SessionFactory sf = cfg.buildSessionFactory();
         Session s = sf.openSession();
         Transaction tx = s.beginTransaction();

         Parent newParent = (Parent) s.load(Parent.class, new Long(1));
         for (Iterator iter = newParent.getChildren().iterator(); iter.hasNext();) {
            Child child = (Child) iter.next();
            System.out.println(child.getName());
         }

         tx.commit();
         s.flush();
         s.close();
         s = null;
         sf.close();
         sf = null;
         cfg = null;


      } catch (Exception e) {
         System.out.println("Exception in Hibernate:" + e.getMessage());
      }
   }
}


Name and version of the database you are using: MySQL 4.0.20


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 4:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/193.html

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 5:12 am 
Newbie

Joined: Tue Apr 19, 2005 2:58 am
Posts: 4
thanks a lot, will try this.

Is that the official way to do it in Hibernate3?

http://forum.hibernate.org/viewtopic.php?t=938636 mentions the article you suggested too, but gavin wrote that this problem is completely fixed in HB3. So I thought that was a workaround for Hibernate2, and there is a better solution for HB3 (which is what I am using).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 8:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh, i didn't know about it, have you tried with last release (3.01)?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 1:16 am 
Newbie

Joined: Tue Apr 19, 2005 2:58 am
Posts: 4
It does not work with 3.0.1 either, but it works fine after adding

public int getRow()
{
return this.getParent().getChildren().indexOf(this);
}

as suggested in the article.

However:


So my conclusion is that this workaround is not the "official" way to do this in HB3. Or maybe I missed some crucial details from the HB3 documentation?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 7:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually you'll need to update your mapping a bit

Code:
Parent
<list inverse="false"/>
</list>

Child
<many-to-one insert="false" update="false"/>


To summarize, you need to let the coçllection be the owner side.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 7:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/193.html up to date

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 12:41 pm 
Newbie

Joined: Tue Apr 19, 2005 2:58 am
Posts: 4
Works for me now, thanks. This should also go into the HB3 documentation, it seems not to be obvious for newbies.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 10:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Year was not for me either ;-)
Open a jira improvement for that please.

_________________
Emmanuel


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