users@glassfish.java.net

Databases have views, JPA has nothing equivalent :/

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Tue, 27 Feb 2007 01:44:18 +0100

Hi there,
I was wondering: databases have 'views' which can be used for clients
if they want only some piece of data from one table of if they wish to
combine columns from few tables to operate on (reading or updating)...
or just because clients does not have to be aware of how does database
structure looks.

The problem is that if one uses JPA, there is nothing equivalent. The
only thing one can do is to create DTO objects and write endless lines
of code that would do same thing as views (gathering data from source
and propagating changes). The problem is that with view you have all
the functionality built in, with view you work basically in the same
way as with tables, while POJO DTOs are to be hard-coded, cannot be
queried, persisted... which makes life VERY VERY hard (instead of
implementing application functionality and business logic, you have to
write countless lines of very stupid code).

I am surprised such a feature is not in JPA from the beginning
(imagine databases without views).

Imagine you want to perform some very simple query, or even less than
that: you just want to get some DTO. Without DTO you write:

em.createQuery("SELECT p FROM Person p WHERE p.something=?1");
and you are done, simple, easy, not very easy to make any mistake.

Now, imagine you have to use DTO:
all you can do, is to use queries like:

Query query = em.createQuery(
 SELECT new com.somecompany.some.project.somepackage.PersonDTO
  (p.firstName, p.lastName, p.somethingElse, p.etc)
 FROM Person p WHERE p.something = ?1);
query.setParameter(1, id);
query.getSingleResult();

WOW - one SIMPLE statement now becomes few long lines of code!!
But that was, actually, the easy part. Before you can replace one
simple #find with few long lines of code you have to prepare your DTO:

a) create Person DTO with many, many fields that are the same as in
Person entity (quite verbose, but well, this is almost OK, still
comparing that to creating a view seems to be very verbose, but one
can live with that)

b) create a super extra huge constructor that can accept every
property and then, in its body, you have to write X lines of code
like:
this.firstName = firstName;
this.lastName = lastName;
this.xyz = xyz;
...many many lines more....

c) now you have to provide some method that can accept PersonDTO and
propagate its properties back into Person entity. That method is
almost exactly the same as the one from (b) but now it copies data
from DTO into entity (aaaaaaargh........ is this for real?)

d) and now every time you want to create DTO, you have to write that
huge SELECT query instead of using simple JPA queries and every
complex query becomes x times more complex.

All that can make thousands lines of code in real application.
All that makes JPA very uncomfortable, verbose and :(


OK, you can choose something else:
your DTO can have only getters and setters, and you can write yourself
some framework for propagating data back and fourth from DTO-Entity
and vice versa using introspection, but things get complicated when
you have to create some data transformations (few percent of fields
needs something more than just being copied).

It is, however very hard to accept, because it is hard to do, and we
should expect such a functionality to be standardized and well tested,
well integrated and, instead of creating business application, we
would have to spend all the time creating framework :/ You can use
some projects like DOZER, but it seems to be way to generic. Imagine -
how much code have to modify when creating one more field/column in
database? (1) database itself (2) peoperty+getter/setter for Entity
(3) add the same to DTO+getter/setter (4) reconfigure DOZER which
means you have to dig inside some XML files, compiler won't help you
if you misspell something.
Terrible.


Are you aware of any such a framework, that could automate the process
of retrieving DTOs from JPA and propagating changes made in DTOs back
into entities? The framework that was designed to cooperate with JPA
and make life easier, so application developers could focus on
business logic?
Is there any on-going JSR os something to enhance JPA, so DTOs would
be as simple to use as views in databases?

What is your opinion about all that?

Regards,
Witold Szczebra