I'm unable to get spring boot to automatically load my database schema when I start it up. I am using MySQL as an external DB. Please find below the code
application properties
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/carsdb
spring.datasource.username=root
spring.datasource.password=password
Application.java
package com.truckla.cars;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAutoConfiguration
@SpringBootApplication
public class CarsApplication {
public static void main(String[] args) {
SpringApplication.run(CarsApplication.class, args);
}
}
Model entity
package com.truckla.cars.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@Table(name = "cars")
@SequenceGenerator(name="seq", initialValue=4, allocationSize=100)
public class Car {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;
String manufacturer;
String model;
Integer build;
public Car() {
}
public Car(Integer id, String manufacturer, String model, Integer build) {
this.id = id;
this.manufacturer = manufacturer;
this.model = model;
this.build = build;
}
public Integer getId() {
return id;
}
public String getManufacturer() {
return manufacturer;
}
public String getModel() {
return model;
}
public int getBuild() {
return build;
}
public void setId(Integer id) {
this.id = id;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public void setModel(String model) {
this.model = model;
}
public void setBuild(Integer build) {
this.build = build;
}
}
schema.sql
CREATE TABLE cars (
id INT AUTO_INCREMENT PRIMARY KEY,
manufacturer VARCHAR(250) NOT NULL,
model VARCHAR(250) NOT NULL,
build YEAR DEFAULT NULL
);
data.sql
INSERT INTO cars (manufacturer, model, build) VALUES
('Ford', 'Model T', 1927),
('Tesla', 'Model 3', 2017),
('Tesla', 'Cybertruck', 2019);
Any ideas what am i doing wrong and what i should change in order to get it work? Thanks in advance.
CodePudding user response:
have you tried with below properties in application-properties file?
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
CodePudding user response:
Try running with the following properties.
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create