Issue
When I am using my Spring Boot application along with JPA to create a table in my database named schooldb, the code runs fine but I am unable to automatically create a table and have to explicitly create it either using the CMD or any other terminal in an IDE.
package com.jpabasics.jpaBasics.enitity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {
@Id
private long studentId;
private String firstName;
private String lastName;
private String emailId;
private String guardianName;
private String guardianEmail;
private String guardianMobile;
}
and here is my POM.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jpabasics</groupId>
<artifactId>jpaBasics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpaBasics</name>
<description>Jpa basics</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Also here is my application.properties file
# Database Config : mysql
spring.datasource.url = jdbc:mysql://localhost:3306/schooldb
spring.datasource.username = root
spring.datasource.password = Lko@6388895330
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Hibernate Configuration
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl.auto = update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
Could someone help me out with this please?
Solution
You're missing the @table annotation which is required to build a table in your DB to host the entities. Also, it's good custom to annotate your columns as well, to make sure they're named correctly (although in your case, they would've been correctly parsed). For example:
package com.jpabasics.jpaBasics.enitity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Table(name="STUDENTS")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "STUDENT_ID")
private long studentId;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "EMAIL_ID")
private String emailId;
@Column(name = "GUARDIAN_NAME")
private String guardianName;
@Column(name = "GUARDIAN_EMAIL")
private String guardianEmail;
@Column(name = "GUARDIAN_MOBILE")
private String guardianMobile;
}
Answered By - Assafs
Answer Checked By - Pedro (JavaFixing Volunteer)