1

Error messages in spring:

: GenerationTarget encountered exception accepting command : Error executing DDL "create table category_class (category_name varchar(255) not null, primary key (category_name)) engine=MyISAM" via JDBC Statement



: GenerationTarget encountered exception accepting command : Error executing DDL "alter table book_class add constraint FKqov24n7bmx5f0s8d74tdxbgbk foreign key (book_category) references category_class (category_name)" via JDBC Statement

Can't exact result set using Spring Boot with Hibernate.

 Caused by: java.sql.SQLSyntaxErrorException: Table 'myDatabase.category_class' doesn't exist

All my other tables are displayed correctly, however my category class does have a string as PK, which may be the cause of the issue. The Error occurs when I try to acess the mysql database with findAll through my categoryService and CategoryRepository which extends JPARepository.

--Category.java--

package myPackage;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Data
@NoArgsConstructor
@Entity
@Table(name="category_class")
public class Category {
        @Id
        @Column(name = "categoryName")
        private String name;

        @OneToMany(mappedBy = "category")
        private List<Book> books;
}

--application.properties--

spring.datasource.url= jdbc:mysql://localhost:3306/oblig2_v3
spring.datasource.username=
spring.datasource.password =
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
server.port=8080

Does anyone know why my table is not getting created in MySQL?

EDIT: Book class added && cleanup.

---Book.java---

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Data
@NoArgsConstructor
@Entity
@Table(name="book_class")
public class Book {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long ISBN; //PK
        private String title;
        private String releaseYear;
        private int quantity;

        @ManyToOne
        @JoinColumn(name ="book_category")
        private Category category;

        @ManyToOne
        @JoinColumn(name ="author")
        private Author author;

        @ManyToMany(mappedBy = "books")
        private List<Order> orders;

}
halfer
  • 19,824
  • 17
  • 99
  • 186
froffen
  • 27
  • 5
  • is the table really named `category_class` or just `category` – Lino Feb 15 '19 at 13:04
  • the error message is quite clear: *Table 'myDatabase.category_class' doesn't exist* – Jens Feb 15 '19 at 13:07
  • @Lino Yes, the table is really named 'category_class'. Used to be 'CATEGORY' but changed it to check if mysql's lowercase convertion was the reason for the table to now show up. Was afraid changing it to 'category' in lowercase would interfere with the '@OneToMany(mappedBy = "category"). If I can pinpoint the issue, the table will problably be changed back to 'category' – froffen Feb 15 '19 at 13:13
  • This is related to a part of your error message (`Specified key was too long; max key length is 1000 bytes`) : https://stackoverflow.com/questions/8746207/1071-specified-key-was-too-long-max-key-length-is-1000-bytes . You may want to avoid having a VARCHAR column as a PK. – Arnaud Feb 15 '19 at 13:28

2 Answers2

0

You need to use the property

spring.jpa.hibernate.ddl-auto=create-drop  or update

The problem in your case is that you already have the database created and it does not make an attempt to update it.

You have two options:

  1. Use Update, this will make spring browse the metadata of your schema and try to apply the nessesary changes.
  2. Drop and re-create the schema each time - this may be desireable in the development phase or in unit/integration test scenarios.

According to spring documentation ,the following values of this property are valid:

You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no schema manager has been detected or none in all other cases. An embedded database is detected by looking at the Connection type. hsqldb, h2, and derby are embedded, and others are not. Be careful when switching from in-memory to a ‘real’ database that you do not make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database.

Alexander Petrov
  • 9,204
  • 31
  • 70
  • Don't think that this answers the question, the error message is still `Table 'myDatabase.category_class' doesn't exist`, so it was never created in the first place – Lino Feb 15 '19 at 13:15
  • @Lino . you don't understand, spring is checking if something has bean created. In this case your schema. When it finds out that something has been created it does not attempt at all to run the re-create. Just try what I told you and you will see. In your case you want to update an already existing schema. – Alexander Petrov Feb 15 '19 at 13:17
  • @Lino You are expecting UPDATE behaviour from CREATE behaviour. It does not work this way. – Alexander Petrov Feb 15 '19 at 13:17
  • This did not seem to solve the issue. However I uploaded a springBoot error which states `GenerationTarget encountered exception accepting command : Error executing DDL "alter table book_class add constraint FKqov24n7bmx5f0s8d74tdxbgbk foreign key (book_category) references category_class (category_name)" via JDBC Statement` on startup – froffen Feb 15 '19 at 13:24
  • Tried Both, the errors seems to be caused by the PK. See the Spring warnings added to top of post. – froffen Feb 15 '19 at 13:30
  • @froffen It is hard to tell if you don't see the exact exception that is thrown ? Can you enable tracing and see what is the exact exception ? – Alexander Petrov Feb 15 '19 at 13:42
0

This was related to having a VARCHAR as PK as @Arnaud stated. Since I didn't want to change my PK, I followed the link provided: #1071 - Specified key was too long; max key length is 1000 bytes

And did the quick fix from one of the answers, creating a database in mysql with:

create database my_db character set utf8 collate utf8_bin; solved my issue.

The link provided should be helpful for anyone else facing similar issues. Thanks for your help.

froffen
  • 27
  • 5