I'm using JPA/Hibernate but probably it doesn't limit question.Currently I'm writing my data access code in my web controller classes. Thanks to JPA, in most cases this code is very simple, basically simple query with trivial setup.I tried to move those methods into separate classes to improve code reuse. But I've encountered an important problem. Although there are similar methods, they are not the same. This especially applies to fetching joined classes (tables). In some controllers I don't need fetching those classes. But in some controllers I do need that. So I have the following variants:Move all methods, even slightly different, as a separate methods into DAO class. This makes DAO complex, their method names will be very long, it would be hard to find correct one.Forget about fetch joins. Setup application to perform loading when needed (lazy loading). This will reduce number of methods, but database access will be much less effective and transaction must span across entire request, not just controller code.Put complex logic into method when needed. E.g. supply boolean parameters fetchSomething. And method will tweak query according to those parameters. Method implementation will be very complex as I would have to either build query string from pieces or use very cumbersome Criteria API. Method will be hard to read and understand.Leave as is. I generally like this approach. It leads to some code duplication, but not very much and queries are declarative so I don't see much problems with that. The main problem is lack of testability. To test controller I would need to prepare a database, because there's no (or I didn't found) complex JPA mock implementation with supplied in-memory data.I wonder what approach used in complex projects? I saw 4-th approach and I saw once very complex in-house ORM based on Hibernate.I'm talking now about so-called repository pattern. I don't really understand what people mean by service layer and never saw any need with that, so currently the question is about data repository classes.
What's the correct approach to DAO layer in presence of ORM framework
orm;repository;hibernate;dao;jpa