We are starting to develop a new web application using JSF (MyFaces on TomEE) and JPA (Eclipselink).
To make develop time faster, we are planning to NOT develop a DTO layer, basically because we do not need it. Following the JSF and Java EE experts advises like Bauke Scholtz How to use DTO in JSF + Spring + Hibernate and Adam Bien How evil are Data Transfer Objects, we'll use JPA entities directly in the presentation layer.
Yet this application must run in a server cluster with sticky session. When a server goes down for maintenance or it is excluded from the cluster for application deploying, user sessions of that server must be served by other servers, without losing the session.
The problem we are facing is that JPA entities that are saved in session (for example in a @ViewScoped bean), are not "completely" replicated on other servers. In fact collection attributes of JPA entities that uses lazy loading are not usable on other servers. When accessing a collection attibute (@OneToMany that use lazy loading) on a server that has the session replica, the exception
org.eclipse.persistence.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship
using indirection that had a null Session.
This often occurs when an entity with an uninstantiated LAZY
relationship is serialized and that relationship is traversed
after serialization.
To avoid this issue, instantiate the LAZY relationship
prior to serialization
is thrown.
I know that EntityManager is not serializable and JPA entities goes completely detached when serialized during session migration, see Struberg's Blog.
So the question is, is there a way to maintain JPA entities in a consistent way in a server cluster, without using EAGER loading?