Hi there ... :-)
I wonder if someone can help me.
I'm trying to retrieve a collection from an object, but I'm getting nothing back. Haven't a clue why this is, because it is stored correctly on the database. I've set up the following mappings:
[code]<hibernate-mapping>
<class name="ScratchPad" table="scratchPad">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<bag name="pageCalendars" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="id"/>
<one-to-many class="PageCalendar"/>
</bag>
<many-to-one name="school"
class="School"
column="schoolId"
not-null="true"/>
</class>
</hibernate-mapping>[/code]
-----------------------------------------------
[code]
<hibernate-mapping>
<class name="Content" table="content">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<bag name="pageCalendars" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="id"/>
<one-to-many class="PageCalendar"/>
</bag>
<many-to-one name="contentType"
class="com.starstream.school.resources.schemas.database.ContentType"
column="contentTypeId"
not-null="true"/>
</class>
</hibernate-mapping>[/code]
----------------------------------------------
[code]
<hibernate-mapping>
<class name="PageCalendar" table="pageCalendar">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="showFrom" column="showFrom" type="date" not-null="false"/>
<property name="showTo" column="showTo" type="date" not-null="false"/>
<property name="active" column="active" type="boolean" not-null="true"/>
<many-to-one name="content" cascade="delete"
class="com.starstream.school.resources.schemas.database.Content"
column="contentId"
not-null="true"/>
<many-to-one name="scratchPad" cascade="delete"
class="com.starstream.school.resources.schemas.database.ScratchPad"
column="scratchPadId"
not-null="true"/>
</class>
</hibernate-mapping>[/code]
I have created a record on the PageCalendar table using the following code:
----------------------------------------------------------------------------------------------
[code]
pageCalendar.setContent(content);
content.getPageCalendars().add(pageCalendar);
pageCalendar.setScratchPad(scratchPad);
scratchPad.getPageCalendars().add(pageCalendar);
try {
logger.debug("Saving page calendar record");
session.flush();
return pageCalendar;
}
[/code]
----------------------------------------------------------------------------------------------
This bit works, because I've had a look at the table, and the
new record has been written, and also has the correct fields filled in
for contentId and ScratchPadId.
But when I do this:
----------------------------------------------------------------------------------------
[code] ScratchPad scratchPad = new ScratchPad();
session.load(scratchPad, new Long(2));
logger.debug("Title ==> " + scratchPad.getName());
logger.debug("Number of calendars ==> " + scratchPad.getPageCalendars().size());[/code]
----------------------------------------------------------------------------------------
The collection inside scratchPad is empty. Even though the correct values are still OK on the database.
I'm obviously missing something here. Can someone help me out with this?
Thanks
|