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)
Related
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
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 !!!
Hi I'm using Spring Boot version 1.5.9.
When using Spring Boot to initialize schema.sql for mysql database, it works all fine and the database schema is getting created successfully. But on restart of the application this schema.sql script is executing again and the application fails to start because the tables already exist.
I tried spring.jpa.hibernate.ddl-auto=create-drop option in application.properties but it does not have any effect (probably because it only works for Hibernate entities which I'm not using)
Is there a way to have Spring Boot to re-create schema from schema.sql every time on restart if the database is not in-memory one?
GitHub:
https://github.com/itisha/spring-batch-demo/tree/database-input
According to the documentation you can simply ignore exceptions by setting spring.datasource.continue-on-error property to true
Spring Boot enables the fail-fast feature of the Spring JDBC
initializer by default, so if the scripts cause exceptions the
application will fail to start. You can tune that using
spring.datasource.continue-on-error.
or even turn it off with spring.datasource.initialize set to false
You can also disable initialization by setting spring.datasource.initialize to false.
A workaround could be, to change the create statements in your schema.sql
from
CREATE TABLE test .....
to
CREATE TABLE IF NOT EXISTS test ...
use the IF NOT EXISTS statements
turn off automatic schema creation to avoid conflicts: add this line in your application.properties
spring.jpa.hibernate.ddl-auto=none
I want to generate DB structure from my Java classes
jpa.generate-ddl: true
jpa.ddl-auto: true
Also, I need to run SQL script before application will up because I have #PostConstruct methods where I use these data.
Can you show an example how to do it in Spring Boot?
A simple spring boot app with the required functionality can be found at.
https://github.com/salilotr89/Spring-boot-postgres-dbinit
Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath).
In addition Spring Boot will load the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform, e.g. you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql etc.).
Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if the scripts cause exceptions the application will fail to start. The script locations can be changed by setting spring.datasource.schema and spring.datasource.data, and neither location will be processed if spring.datasource.initialize=false.
To disable the fail-fast you can set spring.datasource.continue-on-error=true. This can be useful once an application has matured and been deployed a few times, since the scripts can act as ‘poor man’s migrations’ — inserts that fail mean that the data is already there, so there would be no need to prevent the application from running, for instance.
If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
For Reference: Spring Boot - Loading Initial Data
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