Databases have views, JPA has nothing equivalent :/

J

josh

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).

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.find(Person.class,id);
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.id = ?1);
query.setParameter(1, id);
query.getSingleResult();

WOW - one simple statement becomes 6 long lines of code!!
But that was, actually, the easy part. Before you can replace one simple
#find with 6 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;
etc...

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
em.find(Person.class, id);
and every complex query becomes much more complex.

All that can make thousands lines of code in middle-size 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
many places you have to invite when creating one more field/column in
database? (1) database itself (2) add peoperty+getter/setter to Entity
(3) add the same to DTO+getter/setter (4) reconfigure DOZER (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 into
entities? The framework that was designed to cooperate with JPA and make
life easier, so application developers could focus on business logic?
What are your opinion about all that?

Regards,
Witold Szczebra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top