I have a database in mysql called newdb, inside that database there are 2 tables, Track and User.
When I run my springboot application, it creates 2 new tables called track and user and the application uses those tables instead of the 2 tables already in it.
I want it to stop creating and using the tables and use the 2 preexisting tables in the database.
Im not sure what is causing it to create these tables
Here is the Model classes of both tables
Track.java
package net.codejava.song.model;
import org.hibernate.annotations.NaturalId;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.sql.Timestamp;
import java.time.LocalDateTime;
@Entity
@Table(name = "Track")
public class Track {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "isrc", unique = true, nullable = false, length = 20)
private String isrc;
@Column(name = "name", nullable = false, length = 100)
private String name;
@Column(name = "duration_ms", nullable = false)
private Long durationMs;
@Column(name = "explicit", nullable = false)
private Boolean explicit;
@Column(name = "created_at", nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private LocalDateTime createdAt;
public String getIsrc() {
return isrc;
}
public void setIsrc(String isrc) {
this.isrc = isrc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getDurationMs() {
return durationMs;
}
public void setDurationMs(Long durationMs) {
this.durationMs = durationMs;
}
public Boolean getExplicit() {
return explicit;
}
public void setExplicit(Boolean explicit) {
this.explicit = explicit;
}
// default constructor for JPA
public Track() {
this.createdAt = LocalDateTime.now();
}
// constructor with arguments
public Track(String isrc, String name, Long durationMs, Boolean explicit) {
this.isrc = isrc;
this.name = name;
this.durationMs = durationMs;
this.explicit = explicit;
}
}
User.java
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username", unique = true, nullable = false, length = 50)
private String username;
@Column(name = "password", nullable = false, length = 255)
private String password;
// default constructor for JPA
protected User() {}
// constructor with arguments
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// getters and setters
}
here is my application.properties file
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/newdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
here is my pom.xml
pom.xml
<?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>3.0.5</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>net.codejava.song</groupId>
<artifactId>RestAPI-MetaData</artifactId>
<version>1.0</version>
<name>RestAPI-MetaData</name>
<description>A REST-API for storing metadata of music tracks to a Relational Database, fetching the metadata from a mock endpoint.</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>3.10.8</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>3.10.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- MySQL dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- JSON Web Token (JWT) dependency -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<!-- Lombok dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
output of logs of table being created
Hibernate: drop table if exists track
Hibernate: drop table if exists user
Hibernate: create table track (id integer not null auto_increment, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null, duration_ms bigint not null, explicit bit not null, isrc varchar(20) not null, name varchar(100) not null, primary key (id)) engine=InnoDB
Hibernate: create table user (id integer not null auto_increment, password varchar(255) not null, username varchar(50) not null, primary key (id)) engine=InnoDB
Hibernate: alter table track add constraint UK_eeljh0fjup9osa3cp6h9wqtgv unique (isrc)
Hibernate: alter table user add constraint UK_sb8bbouer5wak8vyiiy4pf2bx unique (username)
3
Answers
add this in your application.properties file
Instead of
use
You’ll also need to escape the table name with tics(`) to make it case sensitive.
and
The problem you have is 2 (imho) fold.
To circumvent the first issue you need to enclose your table names in backtics ` to make them case-insensitive (or disable case-sensitive on the MySQL database side of things).
or you could add the properties to the
application.properties
to automatically quote the names of the tables and columns.This should work as well.
If you, instead, want to disable case-sensitive table/columns on the MySQL side of things, read this answer.
Finally it is also advices to not use schema management like that with JPA. At least not for production, so it is probably better to disable it altogether and use something as Flyway or Liquibase to manage your schema instead.
PRO BONUS Your dependencies are a mess, clean them up, else they will come back to haunt you in the future.