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

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

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.

Create DB tables programmatically using Spring or Hibernate

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.

How to edit Hibernate settings in a Spring-Boot project?

Essentially what I'm trying to do is to add this property change to hibernate so I can enable instantiation of composite/embeddable objects when all of its attribute values are null:
hibernate.create_empty_composites.enabled
I am aware that the usual way to edit Hibernate is in the application.properties file like so:
################################################################################
# JPA MANAGEMENT #
################################################################################
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true...
...
spring.jpa.properties.hibernate.create_empty_composites.enabled=true
But the spring.jpa.properties.hibernate.create_empty_composites.enabled=true isn't working. I'm not sure if Spring just doesn't recognize certain properties or if it's just the wrong place to put it.
What I'd like to know is if there is another way to edit the Hibernate properties directly or if there is another fix.
Analysis
The base assumption. More likely you are using Spring Boot 1.5.*.
Spring Boot 1.5.* uses Hibernate 5.0.*. GitHub proof.
Hibernate supports the hibernate.create_empty_composites.enabled setting since the 5.1 version.
GitHub proof.
JIRA proof (?): [HHH-7610] Option for injecting empty (non-null) embedded when all columns are NULL - Hibernate JIRA.
Release notes proof: ORM 5.1 feature release.
Solution
Please consider upgrading the Hibernate dependency in your pom.xml to a more recent version (5.1 and higher).
After that, it should work just fine:
In addition all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created.
— Spring Boot 1.5.* reference, 77. Data Access, 77.5 Configure JPA properties.

Show Sql in spring jdbc applications

I'm writing a plain spring jdbc application using spring boot after a long time without using jpa or hibernate or any other ORM solution. I have need to print the runtime SQL for debugging purposes. I researched a little bit but could not find anything. I know in a jpa/hibernate application we can say following in application.properties
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
But not sure what is the name name of property in spring jdbc applications. Any help is appreciated.

Can JaVers be integrated with Hibernate?

I've seen you can use JaVers to serialize changes to a database, but I can't find a good example about Spring or Hibernate integration. I would also know if I can change the generated table names and columns.
Thank!
In Javers doc, there is the example you are looking for:
http://javers.org/documentation/spring-integration/#spring-jpa-example
If you are using Spring Boot, examples for JaVers Spring Boot starter for SQL
http://javers.org/documentation/spring-boot-integration/
We recommend using the second option - JaVers Spring Boot starter

Resources