To create a table in an H2 database using Spring Boot, you can use the spring.jpa.hibernate.ddl-auto
property in the application.properties
file to specify the database initialization mode.
Here's an example of how you can use the ddl-auto
property to create tables in an H2 database:
# application.properties # create tables based on entity classes spring.jpa.hibernate.ddl-auto=create
This configuration will create tables in the database based on the entity classes in your application. The table names and column names will be derived from the entity class names and field names, respectively.
Note that the ddl-auto
property has several possible values, including create
, create-drop
, update
, and validate
. The create
value will create the tables in the database if they do not exist. The create-drop
value will create the tables and then drop them when the application shuts down. The update
value will update the tables if they already exist, but it will not create them if they do not exist. The validate
value will validate the tables against the entity classes, but it will not make any changes to the database.
For more information about the ddl-auto
property and other configuration options for H2 and Spring Boot, you can refer to the H2 documentation (https://www.h2database.com/html/tutorial.html#using_spring_boot) and the Spring Boot documentation (https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-h2).