Im trying to connect a MySQL database to my java springboot project, but i got a org.hibernate.tool.schema.spi.CommandAcceptanceException error, i already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update, and its still wrong.
Error:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table to_do (id integer not null, desc varchar(255), done bit not null, target_date date, username varchar(255), primary key (id)) engine=InnoDB" via JDBC [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(255), done bit not null, target_date date, username varchar(255), p' at line 1]
Properties:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp logging.level.org.springframework=info logging.level.com.pao.springboot.myFirstWebApp=debug
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/todos spring.datasource.username=todos-user spring.datasource.password=dummytodos spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>glassfish-jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>`
Entity:
ackage com.pao.springboot.myFirstWebApp.toDo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.time.LocalDate;
//Database
// List of todos
@Entity(name = "ToDo")
public class toDo {
@Id
@GeneratedValue
private int id;
private String username;
private String desc;
private LocalDate targetDate;
private boolean done;
public toDo(){}
public toDo(int id, String username, String desc, LocalDate targetDate, boolean done) {
this.id = id;
this.username = username;
this.desc = desc;
this.targetDate = targetDate;
this.done = done;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public LocalDate getTargetDate() {
return targetDate;
}
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
@Override
public String toString() {
return "toDo{" +
"id=" + id +
", user='" + username + '\'' +
", desc='" + desc + '\'' +
", targetDate=" + targetDate +
", done=" + done +
'}';
}
}
I hope someone can help me, thanks to read me.
I already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update.
The cause is simple - DESC
is a reserved keyword in MySQL (and in SQL in general), used to specify descending order, so you may want to name your column explicitly:
@Column(name = "description")
private String desc;
UPD: please don't forget to import corrent @Column:
import jakarta.persistence.Column;