Question 1
This will not be the exact answer to your question, but bear with me ...
Using the Spring DAO pattern, this requires the project to have more than 300 DAO objects and another 300 service classes.
This is a common misconception, and it is wrong.
First of all, it is unlikely that your 300+ tables are independent of each other. Most likely, many of these tables contain the relationships 1-1, 1 - n and n - m. In such cases, it is better to have only a DAO to own part of the relationship. The remaining parts must be stored and retrieved using the cascading capabilities of the JPA.
In addition, the service level is not intended as an additional irrelevant layer on top of your DAOs; it is designed to create a single conceptual domain model, where each user action is mapped to exactly one method at the service level.
Let's look at the following example:
There are three tables: author , book and chapter . There is a 1-n relationship between book and chapter and a nm relationship between book and author . In this example, authors and books are considered strong entities, and chapters are considered weak entities (their existence depends on the existence of books).
In this example, we really have three JPA-annotated POJOs. However, we would only have two DAOs: AuthorDAO and BookDAO . There would be no ChapterDAO , since all access to the chapters must go through BookDAO .
interface BookDAO { findAll(); findById(BookID id) saveBook(Book book); addChapter(BookID id, Chapter chapter);
Now let's look at the ultimate goal of the example. Let's say this is an RSS feed for new books. There would also be an admin interface for adding new books.
In this case, we get two classes of Service - but they are in no way associated with your DAOs!
interface RssService { Collection<Book> getRecentBooks(); } interface AdminService { addBook(AddBookCommand cmd); addAuthor(AddAuthorToBookCommand cmd); }
The last point I am going to make is a moot point, and others will not agree with me. The current AdminService does not provide any means to provide a list of all books in the system. To solve this problem, there are two options:
- Add the
getAllBooks and getAllAuthors methods to the AdminService . - Just use existing DAOs in your views.
I would prefer the latter, but as said, others will not agree with this.
It should be noted, however, that we are implementing these services, they are in no way directly related to your DAOs.
And now I can fully answer your question:
How to write a generic service such as the GenericDAO class above to avoid c / p?
No. Generic services are fully aimed at having a service level.
Question 2
If you look at the DAO implementation, the only thing there is a constructor. Is there a way to have a βsingleβ DAO class that handles all POJOs
Yes, but I advise you again. First, your DAOs may be identical, but as your project progresses, DAOs will specialize and they will need special methods. For instance:
interface BookDAO {
So, now let us leave them as they are, the only constructor and a bunch of common inherited methods.