Spring、Hibernate、EntityManager 和类继承
我对 Spring 和 Spring 还很陌生。冬眠。在我们的团队中,我们正在使用 Struts 2 作为表示层,并在底层使用 Spring 3 和 Hibernate 3(带有注释)来构建 Web 应用程序。我们仍在争论是否使用 DAO 以及 Session
或 EntityManager
进行 CRUD 操作。抛开哪个更好的问题不谈,我在使用 EntityManager 时遇到了另一个问题。
我的目标是创建一个抽象(通用)Service
,它将公开 4 个基本操作:保存、删除、按 ID 查找和按类查找所有操作。其他服务将从中继承并仅实现自定义查找或更新(这将来可能会改变)。问题是,我不确定将带注释的 EntityManager 放在哪里。据我了解,如果我从我的 Action
(即 Bob)调用服务中的 save(),它会从 Person 父级 Service
save() >,系统将无法注入EntityManager
,因为尚未调用Service Person并且未解析@PersistenceContext
注释。
有办法解决这个问题吗?我想对我的服务使用继承,因为这会减少简单操作所需的代码量。
I'm pretty much new to Spring & Hibernate. In our team, we are building a web application using Struts 2 for the presentation layer, with Spring 3 and Hibernate 3 (with Annotations) underneath. We are still debating on whether to use DAOs or not and Session
's or EntityManager
's for CRUD operations. Setting aside the question of which is better than the other, I ran into another problem using EntityManager
.
My goal is to create an abstract (generic) Service
which will expose 4 basic operations: save, delete, find by ID and find all by class. Other services will inherit from this and just implement custom finds or updates (this may change in the future). Problem is, I'm unsure where to put the annotated EntityManager
. I understand that if I call save() in a service from my Action
, i.e. Bob, which inherits the save()
from the Person parent Service
, the system will fail to inject the EntityManager
because the Service Person has not been called and the @PersistenceContext
annotation was not parsed.
Is there a way around this problem? I'd like to use inheritance for my services because that would reduce the amount of code needed for simple operations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此链接可能是有帮助。
如果您最终使用
Session
对象,您需要查看 HibernateDaoSupport 它将很好地用作通用服务类的基类。This link may be helpful.
If you end up using
Session
objects you'll want to look into HibernateDaoSupport which will function nicely as base class for your generic service class.