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.
Related
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
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.
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.
I am getting started with with JPA. I am not running the project in a JEE container, simply using Tomcat. I want to test my DAOs (even though I read somewhere that DAOs may not be relevant with ORMs today) and have minimal dependencies on Spring and Hibernate.
Gist for relevant codes and configuration.
Problems I am facing are
bean of type UserDao cannot be found and hence cannot be injected resulting in error, even though I can see that it is created as it's constructor runs and I have done syso there, BUT
EntityManager is Null when UserDao is initialized
If I change the test-config.xml to use persistence.xml which is configured for MySQL database, it creates all tables, but same errors. hsqldb-persistence.xml is configured to use in-memory db.
Project Structure as it appears in Eclipse. When I run the TestUserDao it read correctly everything from hsqldb-persistence.xml which is in /src/test/java/resources/WEB-INF/ directory.
Appreciate any help.
Thankyou.
What is the difference between:
persistenc.xml can be configured with hibernate properties OR Javax.persistence properties.
What scenarios would you use either one.
I tried using javax.persistence but my spring app would not work.. changed it to hibernate and it started working.
javax.persistence is the standard. Hibernate is an implementation. If you are completely sure that your DB layer will always be handled by Hibernate, go ahead and configure using Hibernate. If not, use javax.persistence, so that you could change your DB layer in the future.