0

updated

Ok, after years of programming I found myself to lack knowledge of how to use JPA properly in your webapp in cases if you use session-scoped forms and lazy loading and deferred commits.

The case is as follows:

  1. Web-app forms are generated once opened and stay session-scoped until explicitly closed or session is destroyed. So, multiple get requests to the same form do nothing but render the form itself.
  2. Form allows user to view/edit bean or hierarchy of beans of the same root (say, User, UserDocuments[] and UserSecurityGroup, last two are referenced from User).
  3. One could open child object from a form of a root object. Children objects are stored in same transaction as the root object is (em.persist(rootObject); using Cascade.ALL).
  4. One could cancel changes made in root/children form until latter is closed.
  5. Components on a form could reference children objects using lazy loading.
  6. Components have custom (configurable) logic (OnClick, OnChange, OnLoad, etc) and can reference (read or modify) latest modified version of edited bean.

According to the brief explanation at TomEE site http://tomee.apache.org/jpa-concepts.html, one can not use JTA persistence in webapp, so, given the fact that lazy loading works only when it's persistence context is opened, one should do all lazy-loading stuff before em.close(). This fact prevents me from implementing points 3 and 6, if I use request-scoped EntityManager. I could solve it by moving EntityManager at session level, but if I do so, I can't satisfy point 4 - changes to children object already been made, object is still managed after form closed. If I move EntityManager to form-scope, application will become jdbc-resource intensive (or not? Does an entity manager create a connection to the database?, and OpenJPA settings at http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_dbsetup_retain).

What other alternatives do I have?

Community
  • 1
  • 1
Askar Kalykov
  • 2,553
  • 1
  • 22
  • 43

2 Answers2

0

If you are using EclipseLink, then access to LAZY relationships is allowed after the persistence context has been closed.

James
  • 17,965
  • 11
  • 91
  • 146
  • And if I modify data while entity's persistence context is closed, how should I save the changes then? – Askar Kalykov Jul 18 '13 at 06:00
  • I've missed condition by which components could access and modify currently edited bean (added to question body). – Askar Kalykov Jul 18 '13 at 08:02
  • You can only make changes while in a transaction. If you change a detached object, you need to merge it into a new persistence context to commit its changes. – James Jul 18 '13 at 13:15
0

Well, I somewhat worked around this problem. If I modify point 3 in a way that I can store draft entries in my tables, then this issue is no longer an issue, and this simplifies whole solution a lot.

I will be glad to see other ideas from the community though :)

Askar Kalykov
  • 2,553
  • 1
  • 22
  • 43