简体   繁体   English

使用JPA附加组件找不到SeedStack“ persistence.xml”

[英]SeedStack “persistence.xml” not found using JPA add-on

I am setting up a SeedStack batch application and I am trying to use JPA without persistence.xml, but automatic JPA detection classes. 我正在设置一个SeedStack批处理应用程序,并且尝试使用不带persistence.xml的JPA,而是使用自动JPA检测类。

However, I had theses exceptions: 但是,我有以下例外:

HHH000318: Could not find any META-INF/persistence.xml file in the classpath 

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named myUnit 

I have this properties in application.yaml: 我在application.yaml中具有以下属性:

    # This configuration file can override main configuration for integration tests
    jdbc:
         datasources:
           myDatasource:
             provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
             url: jdbc:postgresql://localhost:5432/CNVT
             user: postgres
             password : admin
    jpa:
     units:
       myUnit:
         properties:
           hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
           hibernate.hbm2ddl.auto: update

    classes:  
        org:
          generated:
            project:
               domain:
                  model:
                     jpaUnit: myUnit

Also, when I add a persistence.xml, the JPA unit is created: 另外,当我添加persistence.xml时,将创建JPA单元:

o.h.e.t.j.p.i.JtaPlatformInitiator       HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
org.seedstack.jpa.internal.JpaPlugin     Creating JPA unit myUnit from persistence.xml 
org.seedstack.jpa.internal.JpaPlugin     Created 1 JPA unit(s)

But, I had this exception: 但是,我有一个例外:

org.seedstack.seed.SeedException: [SPRING] No spring entitymanager

I would like to use JPA with SeedStack properly without having to do a persistence.xml. 我想将JPA与SeedStack一起正确使用,而不必执行persistence.xml。

Look at the example here , your SeedStack JPA configuration is missing the reference to the datasource: 此处查看示例,您的SeedStack JPA配置缺少对数据源的引用:

    jpa:
     units:
       myUnit:
         datasource: myDatasource
         properties:
           hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
           hibernate.hbm2ddl.auto: update

The absence or presence of the datasource attribute is what makes SeedStack choose between persistence.xml based config or not (but it's not obvious). 缺少datasource属性是使SeedStack在是否基于persistence.xml的配置之间进行选择的原因(但这并不明显)。

In addition, the "No spring entitymanager" exception makes me think you have configured SeedStack to let Spring manage the JPA transactions (using spring.manageJpa config attribute). 另外,“ No springEntitymanager”异常使我认为您已经配置了SeedStack以让Spring管理JPA事务(使用spring.manageJpa config属性)。 If that's the case, it's in contradiction with your SeedStack JPA settings. 如果真是这样,那就与您的SeedStack JPA设置相矛盾。

In a SeedStack/Spring batch, you can either choose to: 在SeedStack / Spring批处理中,您可以选择:

  • Let SeedStack manage JPA. 让SeedStack管理JPA。 In that case you configure JPA with SeedStack and use JPA (including @Transactional annotations) in SeedStack-managed code only (business services, repositories, ...). 在这种情况下,您可以使用SeedStack配置JPA,并仅在SeedStack托管的代码(业务服务,存储库等)中使用JPA(包括@Transactional批注)。
  • Let Spring manage JPA. 让Spring管理JPA。 In that case you configure JPA with Spring (without any SeedStack config) and set spring.manageJpa to true. 在这种情况下,您可以使用Spring配置JPA(不使用任何SeedStack配置),并将spring.manageJpa设置为true。 This will allow SeedStack-managed code to use the Spring-configured JPA transaction manager. 这将允许SeedStack管理的代码使用Spring配置的JPA事务管理器。

Letting Spring manage JPA provides better performance during a Spring batch job, so I recommend the second option (but for batch jobs only). 让Spring管理JPA可以在Spring批处理作业中提供更好的性能,因此我建议使用第二种选择(但仅适用于批处理作业)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM