I have a problem retrieving a fully populated object, I can save but I can't retrieve the object even though i have set the lazy="false" explicityl on the bag definition.
Essentially I have the following java code,
public class AParent implements Serializable
{
// pk
private Pk pk;
// core attributes
...
private List children = new ArrayList();
...
}
Here's the Child class,
public class Child implements Serializable
{
// pk
private Integer childId;
}
All the attributes have getter and setter methods. The Pk class has the following structure,
public class Pk implements Serializable
{
private Integer id;
private Integer status;
}
Again, getter and setter methods for the attributes.
Here is my AParent hibernate mapping file,
<hibernate-mapping>
<!-- the AParent mapping -->
<class name="AParent" table="Parent>
<!-- primary key -->
<composite-id name="Pk" class="Pk">
<key-property name="Id" column="Id" type="integer" />
<key-property name="status" column="status" type="integer" />
</composite-id>
<!-- properties -->
.......
<!-- children-->
<bag name="children" table="Children" cascade="all" lazy="false">
<key>
<column name="Id" />
<column name="status" />
</key>
<many-to-many class="Child">
<column name="childId" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
Here's my child hibernate mapping file,
<hibernate-mapping>
<!-- the Child mapping -->
<class name="Child" table="Child">
<!-- primary key -->
<id name="childId" column="childId" type="integer" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">childIdSeq</param>
</generator>
</id>
<!-- properties -->
...
</class>
</hibernate-mapping>
Now, I can save a AParent instance and all it's children using the following,
public AParent createAParent(AParent parent) throws SQLException
{
Session session = null;
Transaction tx = null;
try
{
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(parent);
tx.commit();
}
catch (Exception e)
{
rollback(tx);
e.printStackTrace();
throw new SQLException();
}
finally
{
close(session);
}
// return the parent
return parent;
}
I have checked the database and it appears that the parent-children-child goes into the database fine.
but when I load it in with the follwing,
public Hotel findAParentByPk(Pk pk) throws SQLException
{
Session session = null;
AParent parent = null;
try
{
session = sessionFactory.openSession();
Query q = session.createQuery("select from Parent where Id = :Id and status = :status");
q.setParameter( "Id", pk.getId());
q.setParameter( "status", pk.getStatus());
parent = q.list().isEmpty() ? null : (AParent)q.list().get(0);
// return the parent
return parent
}
catch (Exception e)
{
e.printStackTrace();
throw new SQLException();
}
finally
{
close(session);
}
}
I don't get the children, even though I have explicitly specified the lazy="false" in the bag definition...
Am I doing something fundamentally wrong?
Cheers,
Ernie
Using hibernate2.jar against oracle 8i
|