I think I'm missing something about Hibernate's goal of transparent persistence and lazy collections. With the exception of lazy collections, I can persist any Entity with no specific Hibernate code in the POJO. This is especially true with an IoC framework like Spring. But as soon as I have a lazy collection, I need Hibernate specific code.
A sample is below in the
getSections method. What am I missing?
Thanks for your time!
Timothy Vogel
Mapping file:
Code:
<hibernate-mapping>
<class name="Course" table="Course" dynamic-update="false" dynamic-insert="false">
<id name="id" column="course_uid" type="long" unsaved-value="null">
<generator class="identity"></generator>
</id>
<version name="updateCount" type="integer" column="updatecount" />
<property name="comment" type="string" update="true" insert="true" column="comment" length="50" />
<property name="name" type="string" update="true" insert="true" column="name" length="50" not-null="true" />
<list name="sections" table="Section" lazy="false" inverse="false" cascade="all-delete-orphan">
<key column="course_uid" />
<index column="sequence" type="integer" />
<one-to-many class="Section" />
</list>
</class>
</hibernate-mapping>
Selected portions of the Course entity:
Code:
/**
* Domain object represeting a course that has multiple sections
*/
public class Course implements Serializable, INamedEntity {
protected final Logger logger = Logger.getLogger(getClass().getName());
private Long id;
private String name;
private List sections;
private int updateCount;
/**
* @return Unique key for this Entity
*/
public final Long getId() {
return this.id;
}
/** An umodifiable (read-only) reference to the sections in this course
* All adds/deletes must be done with methods <code>addSection</code> and <code>removeSection</code> to ensure
* integrity of relationships
* @return List of sections where this class is taught
* @see Course.addSection, Course.removeSection
*/
public final List getSections() {
// Hibernate specific logic
if (!((net.sf.hibernate.collection.List) this.sections).wasInitialized()) {
logger.debug("fetching the sesions List");
// reassociate Course with session / transaction
// fetch the section association explictly
}
return Collections.unmodifiableList(this.sections);
}
/**
* @return version number for this object when it was retrieved from the DB; used for optomistic locking only
*/
public final int getUpdateCount() {
return this.updateCount;
}
}