SeedStack "persistence.xml" not found using JPA add-on - java

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.

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

Related

Unable to set few hibernate properties in Quarkus

I am trying to set this property (hibernate.id.generator.stored_last_used) in application.properties of a quarkus application. But quarkus is ignoring stating that it a Unrecognized configuration key.
How to set few hibernate properties which are not recognized by quarkus-hibernate-orm extension?
I believe you can use the persistence.xml.
In this Quarkus Hibernate ORM guide - Setting up and configuring Hibernate ORM with a persistence.xml is written:
Alternatively, you can use a META-INF/persistence.xml to set up Hibernate ORM. This is useful for:
when you have relatively complex settings requiring the full flexibility of the configuration

How to correctly configure Spring Data JPA into the application.properties configuration file of a Spring Boot 2.X application?

I am finding the following difficulties trying to configure Spring Data JPA into my Spring Boot project.
I have the following problem related to the application.properties file. This is my original application.properties file content:
spring:
application:
name: Spring Boot Excel API
datasource:
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/SOC_Dashboard
username: admin
password: password
timeBetweenEvictionRunsMillis: 1000
testWhileIdle: true
validationQuery: SELECT 1
in which I configured the database connection for my project (I used JdbcTemplate to interact with my databse since now and I am replacing with Spring Data JPA).
I am not so into Spring Boot but it seems to me that exist 2 ways to set the configuration into my application.properties file: one is as done in my configuration (using something like a tree structure) and another one use a "flast" structure.
Searching online I only found this "flat" configuration for JPA:
spring.jpa.hibernate.ddl-auto=none
that is not working in my case. Putting it into my application.properties file I obtain a syntax error due to the fact that it is using the other tree style.
So I am trying to change my original file in this way:
spring:
application:
name: Spring Boot Excel API
datasource:
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/SOC_Dashboard
username: admin
password: password
timeBetweenEvictionRunsMillis: 1000
testWhileIdle: true
validationQuery: SELECT 1
jpa:
hibernate:
ddl-auto: none
Is it the right way to proceed?
Another doubt is related to the ddl-auto configuration. My development is database driven. I design the DB tables and JPA entity have to map these tables. I don't want to create\modify tables starting from my entity. Is it the right configuration?
To answer your first question, YES. It is the right way of configuring spring.jpa.hibernate.ddl-auto configuration in YAML files. And the properties file which you've mention in YAML format. So, the file name should be application.yml. In spring boot, spring-boot-starter-web dependency will automatically include snakeyaml dependency to read YAML files.
For the second question, you can mention none for ddl-auto if you don't want to create tables automatically or you can simply avoid the configuration. Please refer : How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?

How I can migrate to Oracle 12c If I am using Hibernate 3.3 and spring-orm i.e spring-hibernate bootstrapping?

We want to change our database to Oracle 12c 12.2.0.1 from Sybase.
We are currently using Hibernate 3.3 with Spring 4.3.22, We use spring hibernate bootstrapping mode to connect to database i.e getHibernateTemplate() which is having almost 150 occurrences.
After changing Hibernate properties and driver to ojdbc8-12.2.0.1_RHAS64 we found hibernate 3.3 does not provide dialect [org.hibernate.dialect.Oracle12cDialect] for Oracle 12c which is provided in Hibernate 5.XX version.
We were using LocalSessionFactoryBean as part of spring orm in XML appCtxDaos.xml so after upgrading to 5.3 we changed beans class references to XX.XX.hibernate5.localSessionFactoryBean from XX.hibernate3.localSessionFactoryBean which is setting up hibernate configuration for application [Point#1].
After moving to hibernate 5.3, When we checked alternative for getHibernateTemplate(), Hibernate community recommends to use plain Hibernate coding style ( hibernate.cfg.xml ).
We added HibernateUtil.java class which is setting up hibernate configuration for application [Point#2].
Moving ahead I will have to remove Point#1 spring-orm Hibernate configuration, But in our application there are 7-8 xml files which defines application flow and have cascading beans dependency for session Factory, datasource etc
Where database connectivity using point#2 can be achieved but removing point#1 and all dependencies is being challenge.
Please suggest what should be approach to move further to minimal changes in existing application ..
Any contribution to this problem really appreciated !!!

H2 schema still generated when spring.jpa.generate-ddl=false

I am using Spring Boot 2.1.3 with an H2 in memory database for testing. When I run my tests, the schema gets generated even when I specify the following property.
spring.jpa.generate-ddl=false
It seems that because Spring Boot defaults the following property when using H2
spring.jpa.hibernate.ddl-auto=create-drop
That this takes precedence over spring.jpa.generate-ddl=false
Is this a bug?
This behavior is described in the Spring Boot Features documentation in chapter 11.3.3. Creating and Dropping JPA Databases:
By default, the DDL execution (or validation) is deferred until the
ApplicationContext has started. There is also a
spring.jpa.generate-ddl flag, but it is not used if Hibernate
auto-configuration is active, because the ddl-auto settings are more
fine-grained.
Since the property spring.jpa.hibernate.ddl-auto is set by default if Hibernate is used, the flag spring.jpa.generate-ddl is ignored if Hibernate is used (at least with a H2 in-memory database)

Add Hibernate to SpringBoot project

I have a simple Spring Boot project (already mentioned here: Replace hsqldb with MySQL)
I would like to configure Hibernate to work with this project. In another project I used to get EntityManager like so:
#PersistenceContext(unitName = "orm-unit")
private EntityManager em;
but there I also have persistence.xml with required configuration.
In Spring Boot I don't even know where to place any configuration files.
How to make Hibernate work in this case?
Read the Spring Boot documentation. Looking over 31. Working with SQL databases you will see that you need to configure a DataSource.
DataSource configuration is controlled by external configuration
properties in spring.datasource.*. For example, you might declare the
following section in application.properties:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
You can also configure a datasource in a #Configuration mapped class which implements EnvironmentAware.
JHipster generates a cool database configuration using HikariCP. You can check it out the sample here.
For Hibernate you can configure JPA properties.
You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded (default create-drop) or not (default none).
For example to create and drop tables you can add the following to your application.properties.
spring.jpa.hibernate.ddl-auto=create-drop
As for EntityManager when you EnableAutoConfiguration you will trigger a JpaBaseConfiguration which will create an entity manager for you.
You can also use a custom EntityManagerFactory.
To take full control of the configuration of the EntityManagerFactory,
you need to add a #Bean named ‘entityManagerFactory’. Spring Boot
auto-configuration switches off its entity manager based on the
presence of a bean of that type.
And btw you can also use a traditional persistence.xml

Categories