Re-create H2 schema in Hibernate programatically - java

I'd like to use an in-memory db (H2) to preprocess, validate, etc some import-data before persisting it to the "real" production database. Prior to each import I'd like to recreate the H2 schema to have a clean database, as with hibernate.hbm2ddl.auto:"create". Is there any way to call hbm2ddl programatically or how else can I trigger the schema creation?
I'm using Spring Boot 1.3.3 with Hibernate 5.0.5.
Thank you for your help!
Cheers,
Maria

in the meantime I've found the following solution and it worked for me:
https://github.com/valery-barysok/spring4-hibernate5-stackoverflow-34612019
The solution is explained in Programmatic SchemaExport / SchemaUpdate with Hibernate 5 and Spring 4
Thanks,
Maria

Related

Flyway & Hibernate : Cannot populate data to initial database

In a Spring Boot app, I am using Hibernate and 2 tables is created properly. However, I also need to insert data one of these tables and for this purpose I thought I should use Flyway.
Then I just added insert clauses to the Flyway and use the following parameters for Hibernate and Flyway in application.properties:v
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update # also tried none
spring.flyway.url=jdbc:mysql://localhost:3306
spring.flyway.schemas=demo-db
spring.flyway.user=root
spring.flyway.password=******
I have not used Flyway for initializing database and I am not sure if I can use Flyway with Hibernate as I mentioned above. Or, should I disable Hibernate table creation and create another migration script for table creation?
If you use flyway only for insert data don't do that. Try to use this:
With Hibernate:
In addition, a file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop).
With Basic SQL Scripts:
Spring Boot can automatically create the schema (DDL scripts) of your JDBC DataSource or R2DBC ConnectionFactory and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql
The issue here is that Hibernate does not automatically create tables. Additionally, if using Spring Boot, Flyway will run before the service using hibernate has started. As a result, your Flyway script are interacting with a table that does not exist.
The recommended way to do this is to use Flyway to manage both your database structure, your create tables etc, and static data. This will mean your database is versioned and provisioned ready for your service and hibernate can connect.

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 !!!

Why spring-data-jpa is not creating tables in MS SQL server?

When I use in-memory(h2) database, spring-data-jpa is creating tables, but, when I change datasource to point to MS SQL (2017), I cannot see tables getting created.
Can anyone help me to solve this scenario?
Note: I'm using spring boot in my project
Make sure you configured: spring.jpa.hibernate.ddl-auto=update
It's hibernate how creates tables and not Spring Data JPA.

Database and Meta DB table for Spring batch

I am very new to Spring Batch. Tried the getting started example from spring doc. using spring-boot-starter-parent(1.5.2.RELEASE). Trying to understand where can I see the data that is inserted using this "Person" table in hsql. and also where can I see the Meta data tables for this example after execution. Please help me to understand this.
By default, Spring Boot uses an embedded database (H2, HSQL, Derby) accordingly to your dependencies.
H2 provides a great web console to view the state of your database. You'll find more informations here : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-sql-h2-console
If you'd like to stay with HSQL, please have a look at this answer : https://stackoverflow.com/a/8880390/8232755

How liquibase picks up hibernate transactions?

My j2ee application performs db transactions through hibernate. I want to integrate liquibase into my project.
I completed liquibase setup using the link.
I forced hibernate to be read only by setting hibernate.hbm2ddl.auto to none.
Now I run the server and perform insert/update operations. Hibernate doesn't save into database.
I am not understanding how liquibase picks up hibernate db transactions that was called before from configuration file specified in this link.
Am I missing some logic?

Categories