Code First using Spring Boot + Hibernate

Errorlogger
0
Spring Boot gives you defaults on all things, the database is Hibernate, so when you want to change this and use any other database you must define the connection attributes in the application.properties file.
In the sources folder, you create a resource file src/main/resources/application.properties

The set of necessary properties required for generating tables.
**********************************************************************************
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/databasename
spring.jpa.properties.hibernate.default_schema = schemaName
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true

**********************************************************************************
Here, spring.jpa.hibernate.ddl-auto can be none, update, create, create-drop, refer to the Hibernate documentation for details.

  • none This is the default for MySQL, no change to the database structure.
  • update Hibernate changes the database according to the given Entity structures.
  • create Creates the database every time, but don’t drop it when close.
  • create-drop Creates the database then drops it when the SessionFactory closes.

We here begin with create because we don’t have the database structure yet. After the first run, we could switch it to update or none according to program requirements. Use update when you want to make some change to the database structure.

The Hibernate and other embedded databases is create-drop, but for others like MySQL is none

It is good security practice that after your database is in production state, you make this none and revoke all privileges from the MySQL user connected to the Spring application, then give him only SELECT, UPDATE, INSERT, DELETE.

**********************************************************************************

Happy Coding :)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(30)

Our website uses cookies to enhance your experience. Check Now
Accept !