Section 28.6 of the hibernate manual mentions that you shouldn't use entity queries for read only fetching, as they suffer from requesting too much data.
This makes sense, we've seen that complicated domain models can lead to very long running queries, especially when working with table views.
The manual goes on to note:
For read-only transactions, you should fetch DTO projections because they allow you to select just as many columns as you need to fulfill a certain business use case.
But AFAIK JPA doesn't provide a good way for fetching these projections, you are left playing with the criteria builder, which we've found is verbose, brittle and every so often fails to work properly (implementation is hard).
Indeed looking at various posts on SO shows that lots of people are using entities for reads. So the questions I pose are as follows:
- Is avoiding read-only entity queries common in real world enterprise applications, I get the feeling it isn't.
- If it is, what tools are people using to make this bearable, a seperate query dsl comes to mind, jOOQ?
- Are there any resources where best practices are laid out for the architecture of a Spring app with a connection to a persistence layer (I'm thinking of other complicated issues that could cause problems, such as transaction management), it seems everyone seems to take an ad-hoc approach.