-->
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: Duplicate instance in one-to-many association
PostPosted: Wed Nov 23, 2005 7:18 am 
Newbie

Joined: Wed Nov 23, 2005 5:01 am
Posts: 15
Hi,
I have a many-to-one/one-to-many problem between a class and another class that belongs to a class hierarchy. The association is bidirectional. So I have the following hierarchy:
Code:
class Role;
class Patient extends Role;
class Nurse extends Role;

and a class Participation that has many to one association with base class Role.

For hierarchy i use a table per concrete class mapping using union tag as you will see below

I have two entries in Participation table and one in every specific Role table (Patient table and Nurse table) and each entry from Participation table point only to one specific Role entry.

When I try to load a specific Role and to get its participations (you will see in the code below) there is a problem because the size of the participation list is wrong. It's appear to be always 2 instead of 1, and the list contains the same instance of Participation twice (strange).

But what is more intresting is that if I perform debugging on the code I was surprise to see that the result was 1, so it was ok if I had run the debugger step by step.
I have made more tests with the debugger and I have observed that if I put a breakpoint in
Code:
class PojoTuplizer {
...
   protected void setPropertyValuesWithOptimizer(Object object, Object[] values) {
    /**breakpoint HERE*/   optimizer.setPropertyValues( object, values );
   }
}

and I am inspecting the Object[] values, there is no problem and the size of participations list for Role is correct and remain correct after resuming debugging, but if I run again in debug mode and this time I run one more step before inspecting the Object[] values there is a wrong size for that list of participations.
So I was thinking that the calling optimizer has some problems and I had added some code before that call to see if everything it's ok with the Object[] values parameter. The code from PojoTuplizer now looks like:
Code:
   protected void setPropertyValuesWithOptimizer(Object object, Object[] values) {
          for(int i=0;i<values.length;i++) {
                if(values[i] instanceof java.util.List)
                    log.info("Persistence collection "+values[i].getClass().getName()+
                                " has size:"+((java.util.List)values[i]).size());
           }
           optimizer.setPropertyValues( object, values );
   }

After adding the code above all work perfect in debug mode and in normal mode.

Because I am so confuse about this I want to ask you guys if I am doing something wrong.

Thank you very much and regards.

Calin

Hibernate version: hibernate-3.0

Mapping documents:
This mapping is for Role hierarchy
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="bo">
    <class name="Role">
        <id name="id">
            <generator class="guid"/>
        </id>
        <property name="name"/>

        <bag name="part" inverse="true"
                lazy="true" cascade="all">
            <key column="role_id"/>
            <one-to-many class="Participation"/>
        </bag>

        <union-subclass name="Patient" table="Patient">
            <property name="age"/>
        </union-subclass>

        <union-subclass name="Nurse" table="Nurse">
            <property name="function"/>
        </union-subclass>
    </class>
</hibernate-mapping>


and for Participation class is:
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="bo">
    <class name="Participation">
        <id name="id">
            <generator class="guid"/>
        </id>
        <property name="name"/>
        <property name="typeCode" column="typeCode"/>
        <many-to-one name="role" column="role_id" class="Role" cascade="save-update"/>

    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
public void testCreatePartWithRole() {
      session=sf.openSession();
      try{
            Transaction tx=session.beginTransaction();
            Patient p=new Patient();
            p.setName("patient 1");
            p.setAge(22);

            Nurse n=new Nurse();
            n.setName("nurse 1");
            n.setFunction("asistenta");

            Participation pp=new Participation();
            pp.setName("part 1");
            pp.setTypeCode("ceva");
            pp.setRole(p);

            session.saveOrUpdate(pp);

            Participation pp1=new Participation();
            pp1.setName("part 2");
            pp1.setTypeCode("undeva");
            pp1.setRole(n);

            session.saveOrUpdate(pp1);

            tx.commit();
         }finally{
            session.close();
         }

         session=sf.openSession();
         try{
              Transaction tx=session.beginTransaction();
              Query q= session.createQuery("from Role r where r.name like 'patient 1'");
              Role r=(Patient)q.uniqueResult();
              List l=r.getPart();
              System.out.println("Size:"+l.size());

              tx.commit();
          }finally{
              session.close();
          }
}


Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


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