-->
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.  [ 7 posts ] 
Author Message
 Post subject: Transparent persistence and lazy collections
PostPosted: Sun Jan 04, 2004 2:05 pm 
Beginner
Beginner

Joined: Sun Dec 21, 2003 11:19 pm
Posts: 22
Location: Kentucky, USA
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;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 2:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Lazy loading usage always means you need to know the underlying initializer process. In case of Hibernate, this is it's own impleentation of collections.

This does not prevent you from saving an object wo knowing Hibernate, and thus using transparent persistence. Lazy loading usage implies beeing in a session scope.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 1:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You just won an award for the ugliest Hibernate code I have ever seen.

Do not write code like this. Especially, do *not* typecast to internal implementation classes and do *not* access Hibernate from inside your domain model.

Look at the adminapp example application to see how to do this stuff properly, by reassociating needed objects at the beginning of a transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 4:21 pm 
Beginner
Beginner

Joined: Sun Dec 21, 2003 11:19 pm
Posts: 22
Location: Kentucky, USA
I did not like the code either, which is why I posted it looking for an alternative. I will look, again, at the sample apps for a better way.

I will send u a private email with my mailing address so you can mail me my award ;)

Timothy Vogel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 2:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 8:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
:)

A first step would be at least not to cast to a Hibernate collection but to use Hibernate.isInitialized()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:22 pm 
Beginner
Beginner

Joined: Sun Dec 21, 2003 11:19 pm
Posts: 22
Location: Kentucky, USA
Thanks for the suggestion.

Given the feedback from Gavin, I am going to go back and restudy the sample apps.


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