Tag Archives: Hibernate Error

Hibernate Error: Error executing DDL “create table course (xxx)“

Error code:

Hibernate: create table course (id integer not null auto_increment, index integer, name varchar(255), primary key (id)) engine=InnoDB
Oct 12, 2021 4:31:05 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table course (id integer not null auto_increment, index integer, name varchar(255), primary key (id)) engine=InnoDB" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table course (id integer not null auto_increment, index integer, name varchar(255), primary key (id)) engine=InnoDB" via JDBC Statement
...

Hibernate: create table student (id integer not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB
...

Caused by: java.sql.SQLSyntaxErrorException: 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 'integer, name varchar(255), primary key (id)) engine=InnoDB' at line 1
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:762)
	at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:646)
	at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
	... 36 more

Finally, I found that the problem is that an attribute name of my Course class is index, and this is a keyword in MySQL

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "course")
@ManagedBean(name = "course")
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private Integer index;

    @ManyToMany(mappedBy = "courseList")
    private List<Student> studentList;

}

Just change the name of index

Summary: attribute names of entity classes should not be keywords in the database

[Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)

1. Problem Description:

When using hibernate to query the database, the data has been successfully found from the database, but an error is reported when looping through the output: java.lang.stackoverflowerror

2. Solution process:

2.1 this is my query method:

@Test
public void test01() {
     Session session = SessionFactoryUtil.getSessionFactory().openSession();
     String hql = "from Student";

     Query query = session.createQuery(hql);
     List list = query.list();
     list.forEach(System.out::println);
 }

2.2 this is an error message:

java.lang.StackOverflowError
	at java.lang.Integer.toString(Integer.java:402)
	at java.lang.Integer.toString(Integer.java:935)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Course.toString(Course.java:16)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Student.toString(Student.java:25)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Course.toString(Course.java:16)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at java.util.AbstractCollection.toString(AbstractCollection.java:462)
	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at Student.toString(Student.java:25)
	......loop

2.3 check the error message and find that the top exception points to my Lombok annotation: @data

2.4 it is suspected that hibernate does not support Lombok

So I changed all the annotations to setter, getter and toString methods
and then reported the same error. This time, I pointed to the line of outputting courselist in the toString method of my student class

2.5 take a look at my two entity classes:

Student class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ManagedBean(name = "student")
@Entity(name = "student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @ManyToMany
    private List<Course> courseList;

}

Course class:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "course")
@ManagedBean(name = "course")
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private Integer no;

    @ManyToMany(mappedBy = "courseList")
    private List<Student> studentList;
    
}

Note: student and course are many to many, so each class has a collection of each other

2.6 finding problems:

It is not difficult to find that there is a course collection courselist in the student class, and there is also a student collection studentlist in the course class. Therefore, when outputting, it enters a wireless loop process, which is briefly described as follows:

student -> course -> student->course...

3. Solution:

Delete the line that outputs studentlist in the tostring() method in the Course class, and only keep the line that outputs courselist in the student class. Or just keep the output of studentlist in the Course class