I develop a Java application with 4 tiers: database (MySQL), persistence (JPA), business (POJOs similar to stateless session beans in EJB), presentation (Java Swing).
I decided to fully decouple presentation and persistence tiers. So all modifications to all data are done through the business tier. This way the presentation tier does not even know that entity classes exist. This also allows a session bean to have a better control over the data passed, because sometimes a session bean needs to validate or convert values received from the presentation before changing the entities.
However, sometimes a session bean needs to send a big amount of information (like a list of entities with lots of properties) to a caller. And this becomes complicated because according to the design I adopted session bean needs to unwrap all those entities into something else. I tried to convert the list of entities into a list of arrays (where each member of an array correspond to a property in the entity), but this seems to be so flawed, error-prone and inefficient to me.
What would be the right way to send data over to the presentation? Does the concept of hiding the entities behind the session beans make any sense at all? What is the common pattern in such applications?
The common pattern is to use the JPA entities when they fit the bill (i.e. when they contain the data needed by the presentation tier, and when you don’t need to fetch half of the database to return this data), and to use DTOs (Data Transfer Objects, which are just POJOs containing the required information) when the entities don’t fit the bill.
Some prefer to only use DTOs, like you’re doing. But DTOs should be real objects, with typed properties. Lists of arrays a nightmare to understand and maintain, do not offer any kind of type safety and compilation checks, and are not easily usable by the classical presentation tier technologies (JSP EL, etc.), which generally expect JavaBeans.