问题
Hibernate can be used to generate all DAO's and hibernate properties files using reverse engineering. But the DAO code it generates is not appropriate in terms of spring dependency injection.
So, how can we use hibernate reverse engineering technique to generate spring beans based on principle of dependency injection?
回答1:
You should use a generic DAO, where you only need to create a Subclass that specify the entity class. Or a more modern approach like Spring Data JPA or its predecessor Hades. Then the concrete dao in noting more than an empty class (or in case of Spring Data JPA/Hades an empty interface).
So it is not worth spending time in searching and addapting a generator approach for the DAOs. You can write more than sixty of them in one hour by hand (if you have a generic DAO)
I have not so much time to search for an example of an Generic DAO, so this is the best I found: http://www.codeproject.com/Articles/251166/The-Generic-DAO-pattern-in-Java-with-Spring-3-and it has at least one "mistake" the concreate DAO should have the annotation @Repository
but not @Component
. -- Anyway the example illustrate what I mean by generic DAO.
来源:https://stackoverflow.com/questions/9359879/reverse-engineering-in-hibernate-using-spring