1

Although I am fairly experienced in applying JPA to a a web application, I am now facing a challenge in applying it to a desktop application. I could use some pointers!

I working on a JavaFX-based solution, and I'm facing an issue with lazy loading of entity relations. The user interface that I'm after is fairly simple. On one side, there's a listing of entities. Clicking on one of them in the list causes the entity details to be shown on the other side of the screen.

All information needed to display the list is obtained through non-lazy loaded properties of the JPA entities. However, to display the details (after a click), quite a few associated entities need to be retrieved. Given the amount of data, it is not feasible to load all data (eagerly) in memory beforehand, so I'd like the associated entities to be lazy loaded.

Here is where I run into issues with JPA session management. I have not been able to come up with a structured way of having a session active that can be used to lazy load the relations.

In a web-based environment, the session scope is often fairly easy to define. Typically the HTTP request itself can be used to define the lifecycle of a session. The HTTP request forms a very clear start and end point of user interaction. How do you do this in a non-web application?

Guus
  • 2,986
  • 2
  • 21
  • 32
  • Why not use JPA query instead of lazy loading? Besides, if your user starts clicking all over the place, you'll end up with all that data loaded anyway - without being able to garbage collect it? – Alex Nevidomsky Feb 14 '15 at 11:12
  • My advice would be don't use jpa on desktop apps – tomsontom Feb 14 '15 at 12:16

1 Answers1

1

Unless the amount of lazily-loaded data associated with each entity is very large, a list of entities big enough to exceed memory capacity would be unusable from a user perspective.

In ballpark numbers: if each entity used 10KB fully loaded, a list of 10,000 entities (which is completely unusable from the user perspective) would consume 100MB of memory, which is pretty comfortable for a desktop application.

So I would first want to be convinced that lazy initialization was required. If so, perhaps a strategy for limiting the data (e.g. pagination in the list) would be more appropriate.

If you really do want/need lazy initialization, you can use similar approaches to the ones you use in a web application. As @AlexNevidomsky suggests in the comments, a JPA query might be more appropriate than simply accessing the lazily-initialized properties.

For session management (in either case), you can regard the processing of user input (i.e. the event handler) as the boundary of the JPA session (this is basically the equivalent of the HTTP request; it is the response to a user action).

If you are accessing the data repository remotely (or more generally if the request for data may take a long time to process), you will need to process the data request in the background and schedule the UI update on the FX Application Thread when it is complete (though that is something of a different topic).

James_D
  • 201,275
  • 16
  • 291
  • 322