0

I am setting up a SeedStack batch application and I am trying to use JPA without persistence.xml, but automatic JPA detection classes.

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:

    # 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:

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.

1 Answers1

1

Look at the example here, your SeedStack JPA configuration is missing the reference to the datasource:

    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).

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). If that's the case, it's in contradiction with your SeedStack JPA settings.

In a SeedStack/Spring batch, you can either choose to:

  • Let SeedStack manage JPA. In that case you configure JPA with SeedStack and use JPA (including @Transactional annotations) in SeedStack-managed code only (business services, repositories, ...).
  • Let Spring manage JPA. In that case you configure JPA with Spring (without any SeedStack config) and set spring.manageJpa to true. This will allow SeedStack-managed code to use the Spring-configured JPA transaction manager.

Letting Spring manage JPA provides better performance during a Spring batch job, so I recommend the second option (but for batch jobs only).

Adrien LAUER
  • 444
  • 2
  • 7