You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Adam Henderson Azudio <az...@hotmail.com> on 2005/03/07 01:30:58 UTC

Sprin Lazy Load (Again)

List,

I'm having difficulty getting lazy loading to work, I've googled loads 
of pages with examples, blogs and this list about this subject, but I'm 
still hitting a brick wall. I'm using a Matt Raibles' Equinox as my 
starting point.
So here's the situation:
I have a Lesson object with a one to many association to Comment (lazy, 
save-update).
I'm using a LessonManager and LessonDaoHibernate which extends 
HibernateDAOSupport.
I've manually inserted some test records into the db so a lesson has 
one comment associated.
In a save listener method of LessonForm I want to add a Comment to the 
list of comments.
	...
	Comment comment = new Comment();
	comment.setComment("Test Comment");
	comment.setLesson(getLesson());
	// Add the lesson
	getLesson().getComments().add(comment);

	// Now save the Lesson
	getLessonManager().saveLesson(getLesson());
	...

When this method is invoked I get:
java.lang.NullPointerException

Stack Trace:

	 	com.azudio.aptp.web.LessonForm.save(LessonForm.java:82)

thrown at the getLesson().getComments().add(comment) point, surely at 
this point the getComments() method should resolve and return a set 
because when this form page was initialised my manager called my dao 
which retrieved the lesson instance using the session provided by the 
Open Session In View? but other non-collection properties are being 
retrieved.

In web.xml I have:
<!-- Spring Open Session In View Pattern filter -->
   <filter>
     <filter-name>hibernateFilter</filter-name>
     <filter-class>
       org.springframework.orm.hibernate.support.OpenSessionInViewFilter
     </filter-class>
   </filter>

   <!-- Spring/Hibernate filter mappings -->
   <filter-mapping>
     <filter-name>hibernateFilter</filter-name>
     <url-pattern>/app/*</url-pattern>
   </filter-mapping>

Bit of a loss here, any ideas?

Thanks
Adam Henderson



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Sprin Lazy Load (Again)

Posted by Jonathan Millett <jo...@millett.net>.
Another obvious questions ;) Are the comments actually saved to the 
database? It is not clear from your example.

Side Note: In the case that there are no comments (e.g. for a new 
Lesson), lesson.getComments() will return null. In this case, you can 
create a new Set, something like this:

Set comments = new HashSet();
...
comments.add(new Comment("test comment"));
lesson.setComments(comments);
save(lesson); // if cascading is enabled, the comments will be saved too

Adam Henderson Azudio wrote:

> Ryan,
>
> getLesson() is returning a Lesson instance, other non-collection 
> properties are accessible, like getLesson().getTitle()
>
>
> On 7 Mar 2005, at 15:49, Ryan Owens wrote:
>
>> I'm probably being captain obvious here, but a NPE would also happen 
>> on line 7 if getLesson() was returning null.
>> maybe throw in some printlns just to be sure.
>>
>> -Ryan Owens
>>
>>> I'm still getting NPE, so clearly getComments() is returning null.
>>> ...
>>> 1 Comment comment = new Comment();
>>> 2 comment.setComment("Test Comment");
>>> 3 comment.setLesson(getLesson());
>>>
>>> 4 // FORCE REATTACHMENT TO THE SESSION
>>> 5 getLessonManager().saveLesson(getLesson());
>>>
>>> 6 // Add the lesson
>>> 7 getLesson().getComments().add(comment); // FAILS with Null Pointer 
>>> Exception
>>>
>>> 8 // Now save the Lesson
>>> 9 getLessonManager().saveLesson(getLesson());
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Sprin Lazy Load (Again)

Posted by Adam Henderson Azudio <az...@hotmail.com>.
Ryan,

getLesson() is returning a Lesson instance, other non-collection 
properties are accessible, like getLesson().getTitle()


On 7 Mar 2005, at 15:49, Ryan Owens wrote:

> I'm probably being captain obvious here, but a NPE would also happen 
> on line 7 if getLesson() was returning null.
> maybe throw in some printlns just to be sure.
>
> -Ryan Owens
>
>> I'm still getting NPE, so clearly getComments() is returning null.
>> 	   ...
>> 	1    Comment comment = new Comment();
>> 	2    comment.setComment("Test Comment");
>> 	3    comment.setLesson(getLesson());
>>
>> 	4 	// FORCE REATTACHMENT TO THE SESSION
>> 	5	getLessonManager().saveLesson(getLesson());
>>
>> 	6	// Add the lesson
>> 	7    getLesson().getComments().add(comment); // FAILS with Null 
>> Pointer Exception
>>
>> 	8   // Now save the Lesson
>> 	9   getLessonManager().saveLesson(getLesson());
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Sprin Lazy Load (Again)

Posted by Ryan Owens <ry...@infoether.com>.
I'm probably being captain obvious here, but a NPE would also happen on 
line 7 if getLesson() was returning null.
maybe throw in some printlns just to be sure.

-Ryan Owens

> I'm still getting NPE, so clearly getComments() is returning null.
> 	   ...
> 	1    Comment comment = new Comment();
> 	2    comment.setComment("Test Comment");
> 	3    comment.setLesson(getLesson());
>
> 	4 	// FORCE REATTACHMENT TO THE SESSION
> 	5	getLessonManager().saveLesson(getLesson());
>
> 	6	// Add the lesson
> 	7    getLesson().getComments().add(comment); // FAILS with Null 
> Pointer Exception
>
> 	8   // Now save the Lesson
> 	9   getLessonManager().saveLesson(getLesson());


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Sprin Lazy Load (Again)

Posted by Adam Henderson Azudio <az...@hotmail.com>.
Thanks Henri, article makes sense but in practice I can't get it to  
work.

I don't get why I get null pointer exceptions at line 7, here I added a  
line to force reattachment BEFORE the add to the collection, this  
should reattach the lesson to the session, but I'm still getting NPE,  
so clearly getComments() is returning null.
	   ...
	1    Comment comment = new Comment();
	2    comment.setComment("Test Comment");
	3    comment.setLesson(getLesson());

	4 	// FORCE REATTACHMENT TO THE SESSION
	5	getLessonManager().saveLesson(getLesson());

	6	// Add the lesson
	7    getLesson().getComments().add(comment); // FAILS with Null  
Pointer Exception

	8   // Now save the Lesson
	9   getLessonManager().saveLesson(getLesson());
         ...

  I've also tried placing the 'saveLesson' line in pageBeginRender with  
no success.

Stumped.


On 7 Mar 2005, at 03:23, Henri Dupre wrote:

> Have you tried the wiki page I added in tapestry faq?
> http://wiki.apache.org/jakarta-tapestry/FrequentlyAskedQuestions/ 
> SpringHibernate
>
> Henri.
>
>
> On Mon, 7 Mar 2005 00:30:58 +0000, Adam Henderson Azudio
> <az...@hotmail.com> wrote:
>> List,
>>
>> I'm having difficulty getting lazy loading to work, I've googled loads
>> of pages with examples, blogs and this list about this subject, but  
>> I'm
>> still hitting a brick wall. I'm using a Matt Raibles' Equinox as my
>> starting point.
>> So here's the situation:
>> I have a Lesson object with a one to many association to Comment  
>> (lazy,
>> save-update).
>> I'm using a LessonManager and LessonDaoHibernate which extends
>> HibernateDAOSupport.
>> I've manually inserted some test records into the db so a lesson has
>> one comment associated.
>> In a save listener method of LessonForm I want to add a Comment to the
>> list of comments.
>>         ...
>>         Comment comment = new Comment();
>>         comment.setComment("Test Comment");
>>         comment.setLesson(getLesson());
>>         // Add the lesson
>>         getLesson().getComments().add(comment);
>>
>>         // Now save the Lesson
>>         getLessonManager().saveLesson(getLesson());
>>         ...
>>
>> When this method is invoked I get:
>> java.lang.NullPointerException
>>
>> Stack Trace:
>>
>>                  
>> com.azudio.aptp.web.LessonForm.save(LessonForm.java:82)
>>
>> thrown at the getLesson().getComments().add(comment) point, surely at
>> this point the getComments() method should resolve and return a set
>> because when this form page was initialised my manager called my dao
>> which retrieved the lesson instance using the session provided by the
>> Open Session In View? but other non-collection properties are being
>> retrieved.
>>
>> In web.xml I have:
>> <!-- Spring Open Session In View Pattern filter -->
>>    <filter>
>>      <filter-name>hibernateFilter</filter-name>
>>      <filter-class>
>>         
>> org.springframework.orm.hibernate.support.OpenSessionInViewFilter
>>      </filter-class>
>>    </filter>
>>
>>    <!-- Spring/Hibernate filter mappings -->
>>    <filter-mapping>
>>      <filter-name>hibernateFilter</filter-name>
>>      <url-pattern>/app/*</url-pattern>
>>    </filter-mapping>
>>
>> Bit of a loss here, any ideas?
>>
>> Thanks
>> Adam Henderson
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Sprin Lazy Load (Again)

Posted by Henri Dupre <he...@gmail.com>.
Have you tried the wiki page I added in tapestry faq?
http://wiki.apache.org/jakarta-tapestry/FrequentlyAskedQuestions/SpringHibernate

Henri.


On Mon, 7 Mar 2005 00:30:58 +0000, Adam Henderson Azudio
<az...@hotmail.com> wrote:
> List,
> 
> I'm having difficulty getting lazy loading to work, I've googled loads
> of pages with examples, blogs and this list about this subject, but I'm
> still hitting a brick wall. I'm using a Matt Raibles' Equinox as my
> starting point.
> So here's the situation:
> I have a Lesson object with a one to many association to Comment (lazy,
> save-update).
> I'm using a LessonManager and LessonDaoHibernate which extends
> HibernateDAOSupport.
> I've manually inserted some test records into the db so a lesson has
> one comment associated.
> In a save listener method of LessonForm I want to add a Comment to the
> list of comments.
>         ...
>         Comment comment = new Comment();
>         comment.setComment("Test Comment");
>         comment.setLesson(getLesson());
>         // Add the lesson
>         getLesson().getComments().add(comment);
> 
>         // Now save the Lesson
>         getLessonManager().saveLesson(getLesson());
>         ...
> 
> When this method is invoked I get:
> java.lang.NullPointerException
> 
> Stack Trace:
> 
>                 com.azudio.aptp.web.LessonForm.save(LessonForm.java:82)
> 
> thrown at the getLesson().getComments().add(comment) point, surely at
> this point the getComments() method should resolve and return a set
> because when this form page was initialised my manager called my dao
> which retrieved the lesson instance using the session provided by the
> Open Session In View? but other non-collection properties are being
> retrieved.
> 
> In web.xml I have:
> <!-- Spring Open Session In View Pattern filter -->
>    <filter>
>      <filter-name>hibernateFilter</filter-name>
>      <filter-class>
>        org.springframework.orm.hibernate.support.OpenSessionInViewFilter
>      </filter-class>
>    </filter>
> 
>    <!-- Spring/Hibernate filter mappings -->
>    <filter-mapping>
>      <filter-name>hibernateFilter</filter-name>
>      <url-pattern>/app/*</url-pattern>
>    </filter-mapping>
> 
> Bit of a loss here, any ideas?
> 
> Thanks
> Adam Henderson
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org