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.