Evandro Abreu
Evandro Abreu

Reputation: 11

Spring Roo - auto create database tables

I'm using spring roo and would like to set up the project for it to create the bank tables.

how do I do that?

Thank you.

Upvotes: 0

Views: 875

Answers (2)

ardnaxela
ardnaxela

Reputation: 61

If you add

property add --key spring.jpa.properties.hibernate.hbm2ddl.auto --value update in roo script the hibernate property will set in application.properties.

This hibernate property use to create tables and fields in tables in database. - value update it will create an update. - value create it will create (every start of application) - value create-drop it will create and after ending application drop tables

Only tables and fields can be created the database where the tables will created must exist before you start application

Hope it helps

Upvotes: 0

jcgarcia
jcgarcia

Reputation: 3882

First of all I supose that you know how Spring Roo works. Anyway, here's a simple sample about how to create a basic Spring Boot project using the Spring Roo shell. Also, it will help me to show you how to configure it to auto-create tables:

roo> project setup --topLevelPackage com.foo
roo> jpa setup --provider HIBERNATE --database POSTGRES --databaseName postgres_sample --username admin --password admin
roo> entity jpa --class ~.domain.Timer
roo> field string --fieldName message --notNull
roo> repository jpa --all
roo> service --all
roo> web mvc setup
roo> web mvc view setup --type THYMELEAF
roo> web mvc controller --all --responseType THYMELEAF
roo> web mvc controller --all --pathPrefix /api
roo> quit

As you could see, using the jpa setup command I've included the persistence configuration into my project.

After that, your project is ready to be executed. But, if you don't want to create the postgres tables manually and you want to auto-generate the tables, you should include the following property in your application.properties file:

spring.jpa.hibernate.ddl-auto=create-drop

Check the following documentation to know more about this https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-jpa

After that, you could execute the project using the maven command mvn spring-boot:run or using the STS IDE if you prefer. Now, check your postgres database and you will see that a new TIMER table has been generated.

Hope it helps,

Upvotes: 4

Related Questions