How to create database tables from JPA using Hibernate
We know ORM. It will be a nightmare or a bliss based on the angle you are looking at it. This time a bliss
If you have started development from the java side and you have all the entities figured out, then hibernate can create the database for you from the entities.
For this example I will be assuming you are using Spring and Hibernate, with that, how do you create the database from the entities
Initial Setup assumptions
So, I will be assuming you have the spring being setup already, the configuration file for your application would look like:
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/database
jdbc.username = root
jdbc.password = 123
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = create|create-drop|update|none
Here the values to hibernate.hbm2ddl.auto
could be create – if you want to crate during the sessionFactory initiation, create-drop, update when you want to update the exiting database when there is a change on the schema or none if you don’t want to do anything with it.
Could not open Hibernate Session for transaction
Invocation of init method failed – hibernate exception of caching