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
Related
Is there any way to Load the database schema from .sql or json or textfile to create the ORM mapping with JPA/Hibernate to database in spring-boot while starting up the server.
Spring Boot enables you to use database migration tools such as Liquibase and Flyway, you can read more about that on Spring's official documentation.
Edit: From the docs
85.5 Use a Higher-level Database Migration Tool
Spring Boot supports two higher-level migration tools: Flyway and Liquibase.
85.5.1 Execute Flyway Database Migrations on Startup
To automatically run Flyway database migrations on startup, add the org.flywaydb:flyway-core to your classpath.
The migrations are scripts in the form V__.sql (with an underscore-separated version, such as ‘1’ or ‘2_1’). By default, they are in a folder called classpath:db/migration, but you can modify that location by setting spring.flyway.locations. This is a comma-separated list of one or more classpath: or filesystem: locations. For example, the following configuration would search for scripts in both the default classpath location and the /opt/migration directory:
spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration
You can also add a special {vendor} placeholder to use vendor-specific scripts. Assume the following:
spring.flyway.locations=classpath:db/migration/{vendor}
Rather than using db/migration, the preceding configuration sets the folder to use according to the type of the database (such as db/migration/mysql for MySQL). The list of supported databases is available in DatabaseDriver.
FlywayProperties provides most of Flyway’s settings and a small set of additional properties that can be used to disable the migrations or switch off the location checking. If you need more control over the configuration, consider registering a FlywayConfigurationCustomizer bean.
Spring Boot calls Flyway.migrate() to perform the database migration. If you would like more control, provide a #Bean that implements FlywayMigrationStrategy.
Flyway supports SQL and Java callbacks. To use SQL-based callbacks, place the callback scripts in the classpath:db/migration folder. To use Java-based callbacks, create one or more beans that implement Callback. Any such beans are automatically registered with Flyway. They can be ordered by using #Order or by implementing Ordered. Beans that implement the deprecated FlywayCallback interface can also be detected, however they cannot be used alongside Callback beans.
By default, Flyway autowires the (#Primary) DataSource in your context and uses that for migrations. If you like to use a different DataSource, you can create one and mark its #Bean as #FlywayDataSource. If you do so and want two data sources, remember to create another one and mark it as #Primary. Alternatively, you can use Flyway’s native DataSource by setting spring.flyway.[url,user,password] in external properties. Setting either spring.flyway.url or spring.flyway.user is sufficient to cause Flyway to use its own DataSource. If any of the three properties has not be set, the value of its equivalent spring.datasource property will be used.
There is a Flyway sample so that you can see how to set things up.
You can also use Flyway to provide data for specific scenarios. For example, you can place test-specific migrations in src/test/resources and they are run only when your application starts for testing. Also, you can use profile-specific configuration to customize spring.flyway.locations so that certain migrations run only when a particular profile is active. For example, in application-dev.properties, you might specify the following setting:
spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration
With that setup, migrations in dev/db/migration run only when the dev profile is active.
85.5.2 Execute Liquibase Database Migrations on Startup
To automatically run Liquibase database migrations on startup, add the org.liquibase:liquibase-core to your classpath.
By default, the master change log is read from db/changelog/db.changelog-master.yaml, but you can change the location by setting spring.liquibase.change-log. In addition to YAML, Liquibase also supports JSON, XML, and SQL change log formats.
By default, Liquibase autowires the (#Primary) DataSource in your context and uses that for migrations. If you need to use a different DataSource, you can create one and mark its #Bean as #LiquibaseDataSource. If you do so and you want two data sources, remember to create another one and mark it as #Primary. Alternatively, you can use Liquibase’s native DataSource by setting spring.liquibase.[url,user,password] in external properties. Setting either spring.liquibase.url or spring.liquibase.user is sufficient to cause Liquibase to use its own DataSource. If any of the three properties has not be set, the value of its equivalent spring.datasource property will be used.
See LiquibaseProperties for details about available settings such as contexts, the default schema, and others.
There is a Liquibase sample so that you can see how to set things up.
Spring also supports a database initialization on its own, the official docs are here.
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.
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).
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)
I am using Spring Boot Data JPA for my project and would like to add the Hibernate facet. However, this seems to require the use of hibernate.cfg.xml to use for the path property in the facet to work. This completely defeats the purpose of Spring Boot and the auto configury shenanigans it provides. My workaround is to use the JPA facet with Hibernate as the default provider, but I would like to use the Hibernate facet instead. Any help is appreciated.
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