Play Framework 2.6.3 JPA and Lazy Fetching

198 views
Skip to first unread message

Alexis Djamgossian

unread,
Oct 2, 2017, 1:54:32 PM10/2/17
to Play Framework

I am using Play Framework v.2.6.3 , and the JPA solution for SQL access.

When I set the FetchType.Lazy on my entities, I get the following error message:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: java.lang.IllegalArgumentException: failed to lazily initialize a collection of role: models.UserImp.goods, could not initialize proxy - no Session

If you read Play Docs, there it says:

This may mean that your domain object (aggregate root, in DDD terms) has an internal reference to the repository and calls it to return lists of entities and value objects, rather than holding a session open and using JPA based lazy loading.

So, my question is: Whats the best solution to work with Lazy fetching?

I dont get, how do I need to deal with this.

Can you give me an example?

How do you manage your Lazy fetching?, at the moment I am annotating all my fetch types as Eager .

amertum

unread,
Oct 2, 2017, 2:17:16 PM10/2/17
to Play Framework
Usually lazy fetching is interesting to use in your view. In order to do that, you need to extend your hibernate Session up to your view rendering.
In spring MVC there is something to do that but not in play. You need to wrap it up yourself. See https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html
IMHO, you should eager fetch what's needed.
Hope this helps.
Message has been deleted

Alexis Djamgossian

unread,
Oct 2, 2017, 2:28:59 PM10/2/17
to Play Framework
I am using Angular 2 as my front-end framework. 

What you mean is,  use lazy fetching on my view, but still annotate  my class with the fetching type  as Eager?

The problem is: if I dont set the fetching type as Eager, I have a session issue when I use a find method. So, my only option at this point is to use an Eager fetch.

amertum

unread,
Oct 2, 2017, 3:42:17 PM10/2/17
to play-fr...@googlegroups.com
When play receive the request, it goes in the action, then I think you do some work fetching data from the database. Then you convert these data to json and send the response back.

You can access without error the result data from the database query when you're still in the database transaction.

I see it like that :

=> request
  => controller action
    => opening transaction
      => fetching data from database through some repository layer
      => retrieving data from database in some object entity
    => closing transaction
  => converting data to json (should be done before closing transaction if needed)
=> return the response

The problem happened when you're converting the data I suppose. It is because the transaction is closed. If it was open, JPA would fetch more data from the database.
Thus, you need to convert your data to json when still in the transaction. That's what I call "the view". The place where you render you entity to outside.

The other solution you have, is to access the data you need before closing the transaction. Call user.getGoods just after the database query.

That will trigger the lazy loading mechanism to fetch the data. Then you will be able to read it when the transaction is closed.


--
You received this message because you are subscribed to a topic in the Google Groups "Play Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/iGf6wt7Skis/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/19d45c4f-ca0d-405e-9da0-a95fc8bbab11%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Alexis Djamgossian

unread,
Oct 2, 2017, 3:46:21 PM10/2/17
to Play Framework
Ok, that is clear!! thanks.

An assuming that I don't need for example at the moment the Goods of the users, what do i need to do?

Create a criteria query, with only the data that I need?

amertum

unread,
Oct 2, 2017, 4:19:25 PM10/2/17
to play-fr...@googlegroups.com
Yes, you should try. But I don't remember if accessing a non loaded List return null, empty or an error. Because if your tools which convert your entity to json calls all getters, you can have a problem.

--
You received this message because you are subscribed to a topic in the Google Groups "Play Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/iGf6wt7Skis/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages