You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by JulianS <js...@yahoo.com> on 2008/11/03 20:04:17 UTC

Wicket + spring + jpa/ hibernate = lazy load exception

I am experiencing exactly the problem outlined in the subject of this post,
and I would really appreciate any help I can get, as I am under a deadline.
It's the first time I'm using Wicket with JPA, and I just don't understand
why this isn't working. 

I have a Wicket dataprovider that looks like this (I've simplified it a
bit):

public abstract class FooDataProvider extends SortableDataProvider
{
	private static final long serialVersionUID = 1L;

	@SpringBean
	protected MyAPI myApi;
	
	public FooDataProvider()
	{
		super();
		// Injects the spring bean
		InjectorHolder.getInjector().inject(this); 
	}

	public Iterator iterator(final int first, final int count)
	{
		List<Bar> bars = myApi.getBars();
		List<Foo> foos = bars.getFoos();
		return foos.iterator();
	}
}

I am using a very standard Spring JPA setup, and my web.xml includes a
OpenEntityManagerInViewFilter. The spring bean is being injected properly,
and my list of Bar is returned correctly. But I get a
LazyInitializationException no matter what I try. What am I doing wrong?

Many thanks,
Julian

-- 
View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by Igor Vaynberg <ig...@gmail.com>.
On Mon, Nov 3, 2008 at 2:09 PM, JulianS <js...@yahoo.com> wrote:
>
To answer your question, here's my
> implementation of dataprovider#model():
>
>        public IModel model(Object object)
>        {
>                return new Model((Serializable) object);
>        }
>
> Of course, it's very possible I'm missing a fundamental point about loadable
> detachable models...

yes, you are missing a very fundmental point about models and the
reason for your lazy init exception is that you are using Model. there
are quiet a lot of threads on this list that explain and show how to
fix your error. i suggest you search the archives, read the models
page on the wiki, and go to wicketinaction.com and search for "smart
entitymodel".

-igor


>
>
>
> igor.vaynberg wrote:
>>
>> what is your dataprovider#model() look like? are you using a loadable
>> detachable model?
>>
>> -igor
>>
>> On Mon, Nov 3, 2008 at 11:04 AM, JulianS <js...@yahoo.com> wrote:
>>>
>>> I am experiencing exactly the problem outlined in the subject of this
>>> post,
>>> and I would really appreciate any help I can get, as I am under a
>>> deadline.
>>> It's the first time I'm using Wicket with JPA, and I just don't
>>> understand
>>> why this isn't working.
>>>
>>> I have a Wicket dataprovider that looks like this (I've simplified it a
>>> bit):
>>>
>>> public abstract class FooDataProvider extends SortableDataProvider
>>> {
>>>        private static final long serialVersionUID = 1L;
>>>
>>>        @SpringBean
>>>        protected MyAPI myApi;
>>>
>>>        public FooDataProvider()
>>>        {
>>>                super();
>>>                // Injects the spring bean
>>>                InjectorHolder.getInjector().inject(this);
>>>        }
>>>
>>>        public Iterator iterator(final int first, final int count)
>>>        {
>>>                List<Bar> bars = myApi.getBars();
>>>                List<Foo> foos = bars.getFoos();
>>>                return foos.iterator();
>>>        }
>>> }
>>>
>>> I am using a very standard Spring JPA setup, and my web.xml includes a
>>> OpenEntityManagerInViewFilter. The spring bean is being injected
>>> properly,
>>> and my list of Bar is returned correctly. But I get a
>>> LazyInitializationException no matter what I try. What am I doing wrong?
>>>
>>> Many thanks,
>>> Julian
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
>>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20310904.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by JulianS <js...@yahoo.com>.
Igor,

I'm not using a loadable detachable model because I was hoping to get the
list of foos directly from the Bar object, as opposed to having a dedicated
dao method to get the list of foos, which seems to defeat the benefit of
using JPA's object chaining. To answer your question, here's my
implementation of dataprovider#model():

	public IModel model(Object object)
	{
		return new Model((Serializable) object);
	}

Of course, it's very possible I'm missing a fundamental point about loadable
detachable models...



igor.vaynberg wrote:
> 
> what is your dataprovider#model() look like? are you using a loadable
> detachable model?
> 
> -igor
> 
> On Mon, Nov 3, 2008 at 11:04 AM, JulianS <js...@yahoo.com> wrote:
>>
>> I am experiencing exactly the problem outlined in the subject of this
>> post,
>> and I would really appreciate any help I can get, as I am under a
>> deadline.
>> It's the first time I'm using Wicket with JPA, and I just don't
>> understand
>> why this isn't working.
>>
>> I have a Wicket dataprovider that looks like this (I've simplified it a
>> bit):
>>
>> public abstract class FooDataProvider extends SortableDataProvider
>> {
>>        private static final long serialVersionUID = 1L;
>>
>>        @SpringBean
>>        protected MyAPI myApi;
>>
>>        public FooDataProvider()
>>        {
>>                super();
>>                // Injects the spring bean
>>                InjectorHolder.getInjector().inject(this);
>>        }
>>
>>        public Iterator iterator(final int first, final int count)
>>        {
>>                List<Bar> bars = myApi.getBars();
>>                List<Foo> foos = bars.getFoos();
>>                return foos.iterator();
>>        }
>> }
>>
>> I am using a very standard Spring JPA setup, and my web.xml includes a
>> OpenEntityManagerInViewFilter. The spring bean is being injected
>> properly,
>> and my list of Bar is returned correctly. But I get a
>> LazyInitializationException no matter what I try. What am I doing wrong?
>>
>> Many thanks,
>> Julian
>>
>> --
>> View this message in context:
>> http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20310904.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by Igor Vaynberg <ig...@gmail.com>.
what is your dataprovider#model() look like? are you using a loadable
detachable model?

-igor

On Mon, Nov 3, 2008 at 11:04 AM, JulianS <js...@yahoo.com> wrote:
>
> I am experiencing exactly the problem outlined in the subject of this post,
> and I would really appreciate any help I can get, as I am under a deadline.
> It's the first time I'm using Wicket with JPA, and I just don't understand
> why this isn't working.
>
> I have a Wicket dataprovider that looks like this (I've simplified it a
> bit):
>
> public abstract class FooDataProvider extends SortableDataProvider
> {
>        private static final long serialVersionUID = 1L;
>
>        @SpringBean
>        protected MyAPI myApi;
>
>        public FooDataProvider()
>        {
>                super();
>                // Injects the spring bean
>                InjectorHolder.getInjector().inject(this);
>        }
>
>        public Iterator iterator(final int first, final int count)
>        {
>                List<Bar> bars = myApi.getBars();
>                List<Foo> foos = bars.getFoos();
>                return foos.iterator();
>        }
> }
>
> I am using a very standard Spring JPA setup, and my web.xml includes a
> OpenEntityManagerInViewFilter. The spring bean is being injected properly,
> and my list of Bar is returned correctly. But I get a
> LazyInitializationException no matter what I try. What am I doing wrong?
>
> Many thanks,
> Julian
>
> --
> View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by JulianS <js...@yahoo.com>.
Oops...I meant:

		Bar bar = myApi.getBar();
		List<Foo> foos = bar.getFoos();


JulianS wrote:
> 
> I am experiencing exactly the problem outlined in the subject of this
> post, and I would really appreciate any help I can get, as I am under a
> deadline. It's the first time I'm using Wicket with JPA, and I just don't
> understand why this isn't working. 
> 
> I have a Wicket dataprovider that looks like this (I've simplified it a
> bit):
> 
> public abstract class FooDataProvider extends SortableDataProvider
> {
> 	private static final long serialVersionUID = 1L;
> 
> 	@SpringBean
> 	protected MyAPI myApi;
> 	
> 	public FooDataProvider()
> 	{
> 		super();
> 		// Injects the spring bean
> 		InjectorHolder.getInjector().inject(this); 
> 	}
> 
> 	public Iterator iterator(final int first, final int count)
> 	{
> 		List<Bar> bars = myApi.getBars();
> 		List<Foo> foos = bars.getFoos();
> 		return foos.iterator();
> 	}
> }
> 
> I am using a very standard Spring JPA setup, and my web.xml includes a
> OpenEntityManagerInViewFilter. The spring bean is being injected properly,
> and my list of Bar is returned correctly. But I get a
> LazyInitializationException no matter what I try. What am I doing wrong?
> 
> Many thanks,
> Julian
> 
> 

-- 
View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20309299.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by JulianS <js...@yahoo.com>.
James, thanks for your reply. I've checked that my
OpenEntityManagerInViewFilter precedes the Wicket filter. And my debug log
indeed shows that it seems to be opening and closing the entity manager
before and after the wicket filter runs. I'm hoping to avoid the type of
query you suggest, but I'm realizing I may need to resort to it.

Julian


msc65jap wrote:
> 
> Firstly, your code is rather strange. That getFoos() method is not part of
> the List Interface API.
> 
> Two possible solutions:
> 
> The filter chain maybe incorrect in your web.xml. Your
> OpenEntityManagerInViewFilter might not be preceding the Wicket filter.
> Check that it does precede it.
> 
> If you have a deadline then I recommend having one query that retrieving
> both foos and bars in one hit. I've never touched JPQL but in Hibernate
> Query Language (HQL), one could write this:
> 
> "select f from Foo f inner join fetch f.boo b where b.id = :id"
> 
> Good luck,
> James.
> 
> On Mon, Nov 3, 2008 at 7:04 PM, JulianS <js...@yahoo.com> wrote:
> 
>>
>> I am experiencing exactly the problem outlined in the subject of this
>> post,
>> and I would really appreciate any help I can get, as I am under a
>> deadline.
>> It's the first time I'm using Wicket with JPA, and I just don't
>> understand
>> why this isn't working.
>>
>> I have a Wicket dataprovider that looks like this (I've simplified it a
>> bit):
>>
>> public abstract class FooDataProvider extends SortableDataProvider
>> {
>>        private static final long serialVersionUID = 1L;
>>
>>        @SpringBean
>>        protected MyAPI myApi;
>>
>>        public FooDataProvider()
>>        {
>>                super();
>>                // Injects the spring bean
>>                InjectorHolder.getInjector().inject(this);
>>        }
>>
>>        public Iterator iterator(final int first, final int count)
>>        {
>>                List<Bar> bars = myApi.getBars();
>>                List<Foo> foos = bars.getFoos();
>>                return foos.iterator();
>>        }
>> }
>>
>> I am using a very standard Spring JPA setup, and my web.xml includes a
>> OpenEntityManagerInViewFilter. The spring bean is being injected
>> properly,
>> and my list of Bar is returned correctly. But I get a
>> LazyInitializationException no matter what I try. What am I doing wrong?
>>
>> Many thanks,
>> Julian
>>
>> --
>> View this message in context:
>> http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20310970.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket + spring + jpa/ hibernate = lazy load exception

Posted by James Perry <ja...@gmail.com>.
Firstly, your code is rather strange. That getFoos() method is not part of
the List Interface API.

Two possible solutions:

The filter chain maybe incorrect in your web.xml. Your
OpenEntityManagerInViewFilter might not be preceding the Wicket filter.
Check that it does precede it.

If you have a deadline then I recommend having one query that retrieving
both foos and bars in one hit. I've never touched JPQL but in Hibernate
Query Language (HQL), one could write this:

"select f from Foo f inner join fetch f.boo b where b.id = :id"

Good luck,
James.

On Mon, Nov 3, 2008 at 7:04 PM, JulianS <js...@yahoo.com> wrote:

>
> I am experiencing exactly the problem outlined in the subject of this post,
> and I would really appreciate any help I can get, as I am under a deadline.
> It's the first time I'm using Wicket with JPA, and I just don't understand
> why this isn't working.
>
> I have a Wicket dataprovider that looks like this (I've simplified it a
> bit):
>
> public abstract class FooDataProvider extends SortableDataProvider
> {
>        private static final long serialVersionUID = 1L;
>
>        @SpringBean
>        protected MyAPI myApi;
>
>        public FooDataProvider()
>        {
>                super();
>                // Injects the spring bean
>                InjectorHolder.getInjector().inject(this);
>        }
>
>        public Iterator iterator(final int first, final int count)
>        {
>                List<Bar> bars = myApi.getBars();
>                List<Foo> foos = bars.getFoos();
>                return foos.iterator();
>        }
> }
>
> I am using a very standard Spring JPA setup, and my web.xml includes a
> OpenEntityManagerInViewFilter. The spring bean is being injected properly,
> and my list of Bar is returned correctly. But I get a
> LazyInitializationException no matter what I try. What am I doing wrong?
>
> Many thanks,
> Julian
>
> --
> View this message in context:
> http://www.nabble.com/wicket-%2B-spring-%2B-jpa--hibernate-%3D-lazy-load-exception-tp19721199p20308559.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>