LocalContainerEntityManagerFactoryBean 和 LocalEntityManagerFactoryBean 有什么区别?

发布于 2024-11-10 04:10:30 字数 116 浏览 3 评论 0原文

谁能解释一下Spring框架的LocalContainerEntityManagerFactoryBeanLocalEntityManagerFactoryBean之间有什么区别?

Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

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

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

发布评论

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

评论(7

心清如水 2024-11-17 04:10:30

基本上,JPA 规范定义了两种类型的实体管理器。他们是:

i) 应用程序管理:应用程序管理的实体管理器意味着“实体管理器仅由应用程序(即我们的代码)创建和管理”。

ii) 容器管理:容器管理实体管理器意味着“实体管理器仅由 J2EE 容器创建和管理(即我们的代码不直接管理,而是由容器创建和管理实体管理器,并且我们的实体管理器仅由 J2EE 容器创建和管理)”代码通过某种方式获取 EM,例如使用 JNDI )

注意:创建和管理(上面)意味着“在事务中打开、关闭和涉及实体管理器”

LocalContainerEntityManagerFactoryBean - 容器管理
LocalEntityManagerFactoryBean - 应用程序管理

重要说明:对于基于 Spring 的应用程序,差异不大Spring 仅扮演角色(如果您配置LocalContainerEntityManagerFactoryBean,则充当容器;如果您配置LocalEntityManagerFactoryBean<,则充当应用程序 /em>)

Basically JPA specification defines two types of entity managers. They are :

i) Application-Managed : Application Managed entity manager means "Entity Managers are created and managed by merely the application ( i.e. our code )" .

ii) Container Managed : Container Managed entity manager means "Entity Managers are created and managed by merely the J2EE container ( i.e. our code doesn't directly manages instead entity managers are created and managed by container , and our code gets EM's through some way like using JNDI ).

Note : Created and Managed (above) means "opening , closing and involving entity manager in transactions"

LocalContainerEntityManagerFactoryBean - container managed
LocalEntityManagerFactoryBean - application managed

A Big Note : For spring based applications, the difference is not much. Spring only plays roles ( as container if you configure LocalContainerEntityManagerFactoryBean and as application if you configure LocalEntityManagerFactoryBean)

若水微香 2024-11-17 04:10:30

该文档说明了一切:

LocalContainerEntityManagerFactoryBean -- 来自链接:FactoryBean 根据 JPA 的标准容器引导契约创建 JPA EntityManagerFactory。

LocalEntityManagerFactoryBean --从链接:FactoryBean 根据 JPA 的标准独立引导合同创建 JPA EntityManagerFactory。

本质上,唯一的区别在于它们创建 JPA EntityManagerFactory 的方式。

The documentation says it all:

LocalContainerEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard container bootstrap contract.

LocalEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard standalone bootstrap contract.

Essentially, the only difference is in how they create the JPA EntityManagerFactory.

多彩岁月 2024-11-17 04:10:30

LocalEntityManagerFactoryBean

是最简单且最受限制的。您不能引用现有的 JDBC
DataSource bean 定义并且不支持全局事务。

LocalContainerEntityManagerFactoryBean

是最强大的 JPA 设置
选项,允许在应用程序内进行灵活的本地配置。它支持链接到现有的 JDBC 数据源,支持本地和全局事务

REF:spring-framework-reference.pdf“Spring 3”

LocalEntityManagerFactoryBean

is the simplest and the most limited. You cannot refer to an existing JDBC
DataSource bean definition and no support for global transactions exists.

LocalContainerEntityManagerFactoryBean

is the most powerful JPA setup
option, allowing for flexible local configuration within the application. It supports links to an existing JDBC DataSource, supports both local and global transactions

REF: spring-framework-reference.pdf "Spring 3"

通知家属抬走 2024-11-17 04:10:30

LocalEntityManagerFactoryBean 生成应用程序管理的 EntityManagerFactory。

LocalContainerEntityManagerFactoryBean 产生一个容器管理的
实体管理器工厂。

参考:春天在行动 - Craig Walls

LocalEntityManagerFactoryBean produces an application-managed EntityManagerFactory.

LocalContainerEntityManagerFactoryBean produces a container-managed
EntityManagerFactory.

Ref : Spring In Action - Craig Walls

孤独陪着我 2024-11-17 04:10:30
  • 两个实现 LocalEntityManagerFactoryBean 和
    LocalContainerEntityManagerFactoryBean 返回 EntityManagerFactory
    参考自
    org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl。
  • 每个实现都将使用resource_local事务,除非我们
    明确要求 Spring 使用 JTA。
  • 两种实现之间的主要区别是
    LocalContainerEntityManagerFactoryBean 以编程方式提供
    设置持久化单元(数据源&packageToScan),更多
    灵活之处在于我们可以覆盖 persistence.xml 的位置
    文件与 LocalEntityManagerFactoryBean 进行比较,其中我们必须使用预定义的名称 persistence.xml

如果两者都使用 resources_local 作为默认值,那么它并不表示 LocalContainerEntityManagerFactoryBean 使用容器管理的事务而其他使用应用程序管理的事务。

在依赖注入容器外部使用 JPA 时,开发人员需要以编程方式处理事务。如果在 Spring 依赖注入容器内使用 JPA,那么它可以由 Spring 容器处理。

使用 LocalContainerEntityManagerFactoryBean 的示例

public class DataConfig {
    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        //LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        LocalContainerEntityManagerFactoryBean lfb = new LocalContainerEntityManagerFactoryBean();
        lfb.setDataSource(dataSource());
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setPackagesToScan("com.javasampleapproach.h2database.model");
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}
@Component
public class PostRepository {
  @Autowired
    EntityManagerFactory emf;
  }
  public void create(){
      EntityManager em = emf.createEntityManager();
      Post post = new Post("First post");
      em.getTransaction().begin();
      em.persist(post);
      em.getTransaction().commit();
  }
}

LocalEntityManagerFactoryBean 错误

java.lang.IllegalStateException: 不允许在共享 EntityManager 上创建事务 - 使用 Spring 事务或 EJB CMT 代替 使用

public class DataConfig {
    @Bean
    LocalEntityManagerFactoryBean entityManagerFactory() {
        LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}

@Component
    public class PostRepository {
      @Autowired
      EntityManager em;

      public void create(){
          EntityManager em = emf.createEntityManager();
          Post post = new Post("First post");
          em.getTransaction().begin();
          em.persist(post);
          em.getTransaction().commit();
      }
    }
<persistence-unit name="localEntity">
</persistence-unit>

LocalEntityManagerFactoryBean 的工作代码

Spring 管理的事务类似于容器管理对于 LocalEntityManagerFactoryBean。

public class DataConfig {
    @Bean
    LocalEntityManagerFactoryBean entityManagerFactory() {
        LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}

@Component
public class PostRepository {
  @Autowired
  EntityManagerFactory emf;

  @Transactional
  public void create() {
    Post post = new Post("First post");
    em.persist(post);
  }
}

<persistence-unit name="localEntity">
</persistence-unit>

两种实现都可以在容器管理的事务下使用,如果需要一些更正,请纠正我。

  • Both implementations LocalEntityManagerFactoryBean and
    LocalContainerEntityManagerFactoryBean returns EntityManagerFactory
    reference from
    org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.
  • Each implementation will use resource_local transaction unless we
    explicitly ask Spring to use JTA.
  • A major difference between the two implementations is
    LocalContainerEntityManagerFactoryBean provide programmatically
    setting persistence unit (data source & packageToScan), is more
    flexible in that we can override the location of the persistence.xml
    file compare to LocalEntityManagerFactoryBean in which we have to use a predefined name persistence.xml

If both are using resource_local as default then it does not thumb rule that LocalContainerEntityManagerFactoryBean is using container-managed transaction and other is using application-managed transaction.

When using JPA outside of a dependency injection container, transactions need to be handled programmatically by the developer. If using JPA inside of Spring dependency injection container then it can be handled by Spring container.

Example using LocalContainerEntityManagerFactoryBean

public class DataConfig {
    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        //LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        LocalContainerEntityManagerFactoryBean lfb = new LocalContainerEntityManagerFactoryBean();
        lfb.setDataSource(dataSource());
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setPackagesToScan("com.javasampleapproach.h2database.model");
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}
@Component
public class PostRepository {
  @Autowired
    EntityManagerFactory emf;
  }
  public void create(){
      EntityManager em = emf.createEntityManager();
      Post post = new Post("First post");
      em.getTransaction().begin();
      em.persist(post);
      em.getTransaction().commit();
  }
}

Error with LocalEntityManagerFactoryBean

java.lang.IllegalStateException: Not allowed to create a transaction on shared EntityManager - use Spring transactions or EJB CMT instead

public class DataConfig {
    @Bean
    LocalEntityManagerFactoryBean entityManagerFactory() {
        LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}

@Component
    public class PostRepository {
      @Autowired
      EntityManager em;

      public void create(){
          EntityManager em = emf.createEntityManager();
          Post post = new Post("First post");
          em.getTransaction().begin();
          em.persist(post);
          em.getTransaction().commit();
      }
    }
<persistence-unit name="localEntity">
</persistence-unit>

Working code with LocalEntityManagerFactoryBean

Spring managed transaction like container-managed in case of LocalEntityManagerFactoryBean.

public class DataConfig {
    @Bean
    LocalEntityManagerFactoryBean entityManagerFactory() {
        LocalEntityManagerFactoryBean lfb = new LocalEntityManagerFactoryBean();
        lfb.setPersistenceUnitName("localEntity");
        lfb.setPersistenceProviderClass(HibernatePersistence.class);
        lfb.setJpaProperties(hibernateProps());
        return lfb;
    }
}

@Component
public class PostRepository {
  @Autowired
  EntityManagerFactory emf;

  @Transactional
  public void create() {
    Post post = new Post("First post");
    em.persist(post);
  }
}

<persistence-unit name="localEntity">
</persistence-unit>

Both implementations can be used under container-managed transaction, please correct me on this if some correction is need.

东京女 2024-11-17 04:10:30

要在 Spring 项目中使用 JPA,我们需要设置 EntityManager。

这是配置的主要部分,我们可以通过 Spring 工厂 bean 来完成。
这可以是更简单的 LocalEntityManagerFactoryBean 或更灵活的 LocalContainerEntityManagerFactoryBean

baeldung.com/the-persistence-layer-with-spring-and-jpa

To use JPA in a Spring project, we need to set up the EntityManager.

This is the main part of the configuration, and we can do it via a Spring factory bean.
This can be either the simpler LocalEntityManagerFactoryBean or the more flexible LocalContainerEntityManagerFactoryBean.

baeldung.com/the-persistence-layer-with-spring-and-jpa

输什么也不输骨气 2024-11-17 04:10:30

LocalEntityManagerFactoryBean 通过 PersistenceProvider.createEntityManagerFactory() 创建 EntityManagerFactory

LocalContainerEntityManagerFactoryBean 通过 PersistenceProvider.createContainterEntityManagerFactory() 创建 EntityManagerFactory

LocalEntityManagerFactoryBean creates EntityManagerFactory via PersistenceProvider.createEntityManagerFactory()

LocalContainerEntityManagerFactoryBean creates EntityManagerFactory via PersistenceProvider.createContainterEntityManagerFactory()

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