Create DB tables programmatically using Spring or Hibernate - spring

I'm using Spring Boot 2 and Spring Data JPA using Hibernate 5.2. Spring or Hibernate (not sure, which one does it) can create DB tables automatically by setting spring.jpa.hibernate.ddl-auto = create. This automatically uses the connections settings from spring and the #Entity annotated classes to generate a schema for the DB platform I'm using and creates it.
I want to do the same but in code. I want to add a method that automatically uses existing classes and settings to generate the schema SQL and creates the tables. How to do that?
I tried StandardServiceRegistryBuilder and MetadataSources but I have to configure it manually. I assume there should be a way like Spring is doing it by itself.

Related

What does hibernate validate for underlying db on spring application statup

I am not using hibernate validator maven dependency in my springboot project and I really don't see any errors being thrown on application startup when my entity definition doesn't match with the underlying table.
wondering what does hibernate present in the classpath validate on application startup as it pertains to the underlying db
I think it does nothing, until you define a property
spring.jpa.hibernate.ddl-auto=validate
In this case it will cross check your entities with the actual database model.

What is spring boot's databse initialization behaviour with hibernate JPA?

I am using the book Spring in Action 5th edition to learn spring. It had told me to write some jdbc code which was facilitated by H2 database and schema.sql data.sql. It worked. Then it told me to switch to JPA by including spring-boot-starter-data-jpa package in pom.xml. However, I found schema.sql and data.sql were not executed because the database schema is different from what I wrote in schema.sql, and there was no data inside.
I got the impression that hibernate creates tables for me by looking into classes which was annotated with #Entity. It did not work for me. I did some googling and added a property setting to turn the create table action off. however, the schema is different from what the book wrote in schema.sql in previous section. For example, there is a java object field called "createdAt" and was defined in schema.sql as "createdAt" ( the book wrote it this way ), but hibernate expected "created_at".
We have to change the naming strategy to prevent createdAt as created_at
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Note: spring.jpa.hibernate.naming.strategy is not a supported property for Spring JPA implementation using Hibernate 5.
For Spring Boot 1.4.x
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

How to configure multiple datasources in Spring Data JDBC?

I'm trying to replace some projects to use Spring Data JDBC
instead of using JdbcTemplate.
Now, I'd like to use it for multiple DataSources, how can I configure it in Spring Data JDBC?
There is currently no support for working with two or more DataSources. You'd have to manually redo what the JdbcRepositoryFactoryBean does.

How to configure multiple databases in Hibernate with spring boot application

In my project, there is a requirement to use different databases. We are currently using MySql database with Hibernate as ORM layer. And we are hibernate SessionFactory to query the database.
Now there is a requirement to use SQLite database as well. I have found several examples on the internet but they are all using JdbcTemplate.
https://blog.pranavek.com/using-multiple-datasource-in-spring-boot-application/
https://scattercode.co.uk/2016/01/05/multiple-databases-with-spring-boot-and-spring-data-jpa/
But my requirement is to use separate sessionFactories for both the databases.
Please, help me in receiving that.

Spring Boot JPA with not a well known database

I am trying to write Spring Boot application to connect to a Teiid database, I want to use JPA layer on it. I have configured the JDBC Data Source, but since this not well-known database in Spring JPA libraries do not autodetect this source. I have manually setup "spring.jpa.*" properties too. I do have a Hibernate dialect for this database, and it is on the classpath.
So, how does one need to configure JPA layer for a not well-known database in Spring Boot? Thank you for your time.
Ramesh..
This is fairly well defined in the Spring Boot documentation.
You can set this explicitly in the application.properties file
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Resources