Home > Blockchain >  Table doesn't get created JPA
Table doesn't get created JPA

Time:03-19

Table "books" doesn't get created somehow.

My books table:

@Entity
@Table(name = "books")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "publisher_id", nullable = false)
private Publisher publisher;

@ManyToMany
@JoinTable(name = "authors_books", joinColumns = @JoinColumn(name = "book_id"),
        inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors;

@Column(name = "is_rented", nullable = false, columnDefinition = "DEFAULT FALSE")
private Boolean is_rented;
  
@Column(name = "isbn", nullable = false, unique = true, length = 300)
private String isbn;

Publishers table:

@Entity
@Table(name = "publishers")
public class Publisher {
   @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @OneToMany(mappedBy = "publisher")
    private Set<Book> books;

Authors table:

@Entity
@Table(name = "authors")
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToMany(mappedBy = "authors")
    private Set<Book> books;

application.properties:

spring.jpa.hibernate.ddl-auto=create
spring.jpa.database=mysql
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/<schema_name>?createDatabaseIfNotExist=true
spring.datasource.username=${MYSQL_USERNAME}
spring.datasource.password=${MYSQL_PASSWORD}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Omitted other fields, only reference fields left. I also ovverrid equals and hashcode methods in all entities. And I have empty constructors and full constructors, as well as getters and setters in all of them. Also added @EntityScan to SpringBootApplication file.

I get error:

Table '<schema_name>.authors_books' doesn't exist.
Table '<schema_name>.books' doesn't exist.

But all other tables do exist. Does anybody see what I am missing?

EDIT

Checked database manually. Table "authors_books" DOES exist(despite jpa telling me that it doesn't). Only "books" DOES NOT.

EDIT #2

I added to application.properties:

spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true

And it shows me:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
    create table books (
       id bigint not null,
        amount integer not null,
        image varchar(300),
        is_rented DEFAULT FALSE,
        isbn varchar(300) not null,
        published_on date,
        title varchar(300) not null,
        publisher_id bigint not null,
        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 'DEFAULT FALSE, isbn varchar(300) not null, ' at line 5

CodePudding user response:

Looks like problem was with:

@Column(name = "is_rented", nullable = false, columnDefinition = "DEFAULT FALSE")
private Boolean is_rented;

If you configure columnDefinition, then Hibernate does not care about java-based data type you provided.

So the SQL was like:

is_rented DEFAULT FALSE,
  • which obviously lacks data type.

So I changed it into:

@Column(name = "is_rented", columnDefinition = "BOOLEAN DEFAULT FALSE")
private Boolean is_rented;

And it worked :)

  • Related