I\'m developing a new Java web application and I\'m exploring new ways (new for me!) to persist the data. I mostly have experience with JPA & Hibernate but, except for simpl
The last ten years I was using JDBC, EJB entity beans, Hibernate, GORM and finally JPA (in this order). For my current project I have returned to using plain JDBC, because the emphasis is on performance. Therefore I wanted
The data model is defined in a data dictionary; using a model driven approach a generator creates helper classes, DDL scripts etc. Most operations on the database are read-only; only few use cases write.
Question 1: Fetching children
The system is built on a use cases, and we have one dedicated SQL statement to get all data for a given use case/request. Some of the SQL statments are bigger than 20kb, they join, calculate using stored functions written in Java/Scala, sort, paginate etc. in a way that the result is directly mapped into a data transfer object which in turn is fed into the view (no further processing in the application layer). As a consequence the data transfer object is use case specific as well. It only contains the data for the given use case (nothing more, nothing less).
As the result set is already "fully joined" there is no need for lazy/eager fetching etc. The data transfer object is complete. A cache is not needed (the database cache is fine); the exception: If the result set is large (around 50 000 rows), the data transfer object is used as a cache value.
Question 2: Saving
After the controller has merged back the changes from the GUI, again there is a specific object which holds the data: Basically the rows with a state (new, deleted, modified, ...) in a hierarchy. It's a manual iteration to save the data down the hierarchy: New or modified data is persisted using some helper classes with generate SQL insert
or update
commands. As for deleted items, this is optimized into cascaded deletes (PostgreSql). If multiple rows are to be deleted, this is optimized into a single delete ... where id in ...
statement as well.
Again this is use case specific, so it's not dealing with a general approch. It needs more lines of code, but these are the lines which contain the optimizations.
The experiences so far
Related information:
Update: Interfaces
If there is a Person
entity in the logical data model, there is a class to persist a Person
entity (for CRUD operations), just like a JPA entity.
But the clou is that there is no single Person
entity from the use case / query / business logic perspective: Each service method defines its own notion of a Person
, and it only contains exactly the values required by that use case. Nothing more, nothing less. We use Scala, so the definition and usage of many small classes is very efficient (no setter/getter boiler plate code required).
Example:
class GlobalPersonUtils {
public X doSomeProcessingOnAPerson(
Person person, PersonAddress personAddress, PersonJob personJob,
Set personFriends, ...)
}
is replaced by
class Person {
List addresses = ...
public X doSomeProcessingOnAPerson(...)
}
class Dto {
List persons = ...
public X doSomeProcessingOnAllPersons()
public List getPersons()
}
using use case specific Persons
, Adresses
etc: In this case, the Person
already aggregates all relevant data. Requires more classes, but there is no need to pass around JPA entities.
Note that this processing is read-only, the results are used by the view. Example: Get distinct City
instances from a person's list.
If data is changed, this is another use case: If the city of a person is changed, this is processed by a different service method, and the person is fetched again from the database.