Spring、Hibernate、EntityManager 和类继承

发布于 2024-08-25 00:26:36 字数 619 浏览 3 评论 0原文

我对 Spring 和 Spring 还很陌生。冬眠。在我们的团队中,我们正在使用 Struts 2 作为表示层,并在底层使用 Spring 3 和 Hibernate 3(带有注释)来构建 Web 应用程序。我们仍在争论是否使用 DAO 以及 SessionEntityManager 进行 CRUD 操作。抛开哪个更好的问题不谈,我在使用 EntityManager 时遇到了另一个问题。

我的目标是创建一个抽象(通用)Service,它将公开 4 个基本操作:保存、删除、按 ID 查找和按类查找所有操作。其他服务将从中继承并仅实现自定义查找或更新(这将来可能会改变)。问题是,我不确定将带注释的 EntityManager 放在哪里。据我了解,如果我从我的 Action(即 Bob)调用服务中的 save(),它会从 Person 父级 Servicesave() >,系统将无法注入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-09-01 00:26:36

链接可能是有帮助。

如果您最终使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文