使用 spring 在 hibernate 中进行逆向工程
Hibernate 可用于通过逆向工程生成所有 DAO 和 hibernate 属性文件。 但它生成的 DAO 代码对于 spring 依赖注入来说并不合适。
那么,我们如何利用hibernate逆向工程技术来生成基于依赖注入原理的spring bean呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用通用 DAO,其中您只需要创建一个指定实体类的子类。或者更现代的方法,例如 Spring Data JPA 或其前身 哈迪斯。然后,具体的 dao 不仅仅是一个空类(或者在 Spring Data JPA/Hades 的情况下是一个空接口)。
因此,不值得花时间为 DAO 搜索和调整生成器方法。您可以在一小时内手写其中六十多个(如果您有通用 DAO)
我没有那么多时间来搜索通用 DAO 的示例,所以这是我找到的最好的:http://www.codeproject.com/Articles/251166/The-Generic-DAO-pattern-in-Java-with-Spring-3-并且它至少有一个“错误” " concreate DAO 应该具有注释
@Repository
,但不具有@Component
。 -- 无论如何,这个例子说明了我所说的通用 DAO 的含义。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.