-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can't fetch collection
PostPosted: Sat May 08, 2004 9:36 am 
Newbie

Joined: Fri May 07, 2004 9:45 am
Posts: 3
Hi!

I am having difficulties with the fetching of a collection. Although the data is in the database, the getter method doesn't retrieve it.

It comes down to this example:

child.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="logic.Child" table="child">
        <id name="id" type="string" unsaved-value="null" >
            <column name="id" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
        <property name="menge"/>
     <many-to-one name="parent" class="logic.Parent" column="parent" not-null="true"/>
    </class>
</hibernate-mapping>


parent.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="logic.Parent" table="parent">
        <id name="id" type="string" unsaved-value="null" >
            <column name="id" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
        <property name="name"/>
     <set name="children" inverse="true" cascade="all-delete-orphan">
        <key column="parent"/>
             <one-to-many class="logic.Child"/>
        </set>
    </class>
</hibernate-mapping>


Database: MySQL 4.1.0
Schema:
Code:
drop table if exists child;
drop table if exists parent;
create table child (
   id char(32) not null,
   menge INTEGER,
   parent VARCHAR(255) not null,
   primary key (id)
);
create table parent (
   id char(32) not null,
   name VARCHAR(255),
   primary key (id)
);
alter table child add index (parent), add constraint FK5A3F51CC4AB08AA foreign key (parent) references parent (id);


Now I enter some test data into the database:
Code:
Session session = HibernateUtil.currentSession();
Transaction tx;
tx = session.beginTransaction();
         
Parent p = new Parent();
p.setName("Parent");
session.save(p);
session.flush();

Child c1 = new Child(1);
p.addChild(c1);
session.save(c1);
session.flush();
         
Child c2 = new Child(2);
p.addChild(c2);
session.save(c2);
session.flush();

// just for safety, problem still exists when I leave this out
session.update(p);
session.flush();
         
tx.commit();


With the Parent.addChild method being:
Code:
public void addChild(Child c) {
    c.setParent(this);
    children.add(c);   
}


Then I try to fetch the data again:
Code:
Session session = HibernateUtil.currentSession();
Transaction tx;
tx = session.beginTransaction();

Query q = session.createQuery("from logic.Parent p where p.name = :name");
q.setParameter("name", "Parent");
List result = q.list();
Parent p = (Parent) result.get(0);

System.out.println("ID: " + p.getId());
System.out.println("# of children: " + p.getChildren().size());

session.flush();
tx.commit();



When I fetch the data right after inserting it, it all runs fine (2 children found). When I restart the program, skip the inserting part and just fetch the data, the output is as follows:

Code:
Hibernate: select parent0_.id as id, parent0_.name as name from parent parent0_ where (parent0_.name=? )
Hibernate: select children0_.id as id__, children0_.parent as parent__, children0_.id as id0_, children0_.menge as menge0_, children0_.parent as parent0_ from child children0_ where children0_.parent=?
ID: 2c960393fc6460d200fc6460dd110001
# of children: 0


Hibern8IDE also shows an empty "children" collection.
However, the data is stored correctly in the database, all columns are filled with the appropriate (foreign) keys, and the Hibernate SQL command from the debug output fetches -when entered manually- the children's IDs.

Maybe this is the same problem Rayz has encountered:
http://forum.hibernate.org/viewtopic.php?t=930435

I know there must be some stupid mistake, but I can't find it. Thanks for any help.


Top
 Profile  
 
 Post subject: Additonal info
PostPosted: Sat May 08, 2004 10:47 am 
Newbie

Joined: Fri May 07, 2004 9:45 am
Posts: 3
This query gives me the children, p.getChildren() doesn't. Looks like a mapping problem.

Code:
List result = session.createSQLQuery("SELECT {c.*} FROM child AS {c} WHERE parent = '" + p.getId()+ "'","c",Child.class).list();


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 09, 2004 1:09 am 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
I don't know what sort of logging framework you are using, but if you are using log4j or jdk1.4 logging, and set the log level to DEBUG, Hibernate should spit out a record of the objects as they are hydrated, and the values being bound to each field. Hibernate will also spit out exactly what values are being bound to the PreparedStatement parameters. This sort of information might give you an idea as to where the problem is occurring.

Scott


Top
 Profile  
 
 Post subject: Problem solved
PostPosted: Fri May 14, 2004 3:24 pm 
Newbie

Joined: Fri May 07, 2004 9:45 am
Posts: 3
Thanks a lot.

I solved the problem: changing the id-tag to

Code:
<id name="id" column="id">
   <generator class="native"/>
</id>


did the job.


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