i´m currently learning Spring Boot, and trying to set up JPA with a MySQL database. Everything looks fine, the project starts correctly, no visible errors, but…no table is created on my db. I´ve checked everywhere and everything seems fine, i have no clue what´s causing that behaviour…
I´ll leave the relevant parts of the code:
ProjectApplication.java:
package com.first.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
My Student class:
package com.first.project.student;
import org.springframework.data.annotation.Id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import java.time.LocalDate;
@Entity
@Table
public class Student {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private Long id;
private String name;
private String email;
private LocalDate dateOfBirth;
private Integer age;
public Student() {
}
public Student(Long studentId, String studentName, String studentEmail, LocalDate studentDoB, Integer studentAge) {
this.id = studentId;
this.name = studentName;
this.email = studentEmail;
this.dateOfBirth = studentDoB;
this.age = studentAge;
}
public Student(String studentName, String studentEmail, LocalDate studentDoB, Integer studentAge) {
this.name = studentName;
this.email = studentEmail;
this.dateOfBirth = studentDoB;
this.age = studentAge;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", email='" + email + ''' +
", dateOfBirth=" + dateOfBirth +
", age=" + age +
'}';
}
}
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/(my database)
spring.datasource.username=(my username)
spring.datasource.password=(my password)
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.generate-ddl=true
The dependencies from my pom.xml file:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
Inside the main package i have ProjectAplication, and a subpackage with the Student class inside.
Any thoughts?
2
Answers
In the application.properties file
Here, spring.jpa.hibernate.ddl-auto=create-drop means that the application creates tables when it starts and drops them when it exits. To view the tables after the application ends, you can change it to spring.jpa.hibernate.ddl-auto=update. The modified configuration would look like this:
With this change, the application retains the tables after it exits, allowing you to view them later.
you use create-drop so table is created when you start application and dropped when you stop and if your application is runnig and still doesn’t show table so try to do this : check database name is match to spring database , and try to do refresh in mysqlworkbench