Unable to get spring boot to automatically create database schema
There are several possible causes:
-
Your entity classes are in the same or in a sub-package relative one where you have you class with
@EnableAutoConfiguration.
If not then your spring app does not see them and hence will not create anything in db -
Check your config, it seems that you are using some hibernate specific options, try to replace them with:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=test spring.datasource.password=
**note that the manual loading of the driver class is unnecessary because it's automatically registered, so don't bother yourself with it
- Your
application.properties
must be insrc/main/resources
folder.
If you did not specify dialect correctly it might try to default to bundled together with boot in-memory database and (as it was with me) I could see that it tries to connect to local HSQL
(see console output) instance and fail at updating the schema.
Did you try running it with:
spring.jpa.generate-ddl=true
and then
spring.jpa.hibernate.ddl-auto = create
By default the DDL execution (or validation) is deferred until the ApplicationContext has started. There is also a spring.jpa.generate-ddl flag, but it is not used if Hibernate autoconfig is active because the ddl-auto settings are more fine-grained.
see spring-boot-features
@SpringBootApplication
@EnableConfigurationProperties
@EntityScan(basePackages = {"com.project.ppaa.model"}) // scan JPA entities
public class Application {
private static ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
Application.applicationContext = SpringApplication.run(Application.class, args);
}
}
it should work automatically but if it does not, you can enter the base package
@EntityScan(basePackages = {"com.project.ppaa.model"}) // scan JPA entities manually
Using the following two settings does work.
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create