实体管理器Spring事务问题

发布于 2024-10-09 10:19:39 字数 280 浏览 0 评论 0原文

我对概念有疑问,我想我正在混合一些东西。我有一个带有 DAO 层的应用程序,这些 DAO 的实现使用 SessionFactory (Hibernate) 来执行 CRUD 操作,我还有一个服务层并使用 Spring Declarative Transaction 管理我的工作单元,一切工作正常,我不确定是否使用 EntityManager,但它似乎所做的是管理我的工作单元(与我使用 Spring 做的事情相同)。

我什么时候应该使用EntityManager? 使用它有什么好处? 我为什么要使用它?

谢谢。

i have a doubt on concept i think i am mixing some stuff. I have a application with DAO layer the implementation of those DAO use SessionFactory (Hibernate) to execute the CRUD operation, i also have a service layer and manage my unit of work with Spring Declarative Transaction, and everything work fine, i am not sure about wether to use EntityManager or not but it seems that what it does is manage my unit of work (same thing that i am doing with Spring).

When should i use EntityManager?
What is the advantages of using it?
Why Should i use it?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

蛮可爱 2024-10-16 10:19:39

EntityManager 对于 JPA 就像 SessionFactory 对于 Hibernate 一样。即使您使用 JPA,Spring 仍然可以管理您的事务。如果您的 JPA 提供者是 Hibernate,那么您可以安全地混合搭配 Hibernate 和 JPA,因为它们可以一起工作。

现在讨论何时使用 EntityManager 及其优点的问题:

如果您正在开始一个新项目并且可以使用 JPA 2,那么我建议您开始使用它,因为 JPA 是规范(意味着如果您坚持规范,那么您可以在不更改代码的情况下更改实现。Eclipseling、DataNucleus、OpenJPA 是 JPA 规范的一些示例实现。如果您无法迁移到 JPA 2,那么我建议您坚持使用 Hibernate。

另请浏览以下帖子

Hibernate/Toplink 在 JPA 之上提供什么?

JPA 或 Hibernate 用于 Java 持久性?

JPA 1 不够好

EntityManager is for JPA what SessionFactory is for Hibernate. Spring can still manage your transaction even if you are uing JPA. If your JPA provider is Hibernate then you can mix and match Hibernate and JPA stuff safely as they both work together.

Now coming to the question of When to use EntityManager and the advantages:

If you are starting a new project and if you can use JPA 2 then I suggest you start using it as JPA is the spec (means that if you stick to the spec then you can change the implementations without changing the code. Eclipseling, DataNucleus, OpenJPA are some example implementations of JPA spec). If you can't move to JPA 2 then I suggest you stick to Hibernate.

Also go through the below posts

What does Hibernate/Toplink offer above JPA?

JPA or Hibernate for Java Persistence?

JPA 1 is not good enough

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