Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot/Hibernate fails creating table

I am getting this warn when I try to start my spring boot application:

2018-09-30 07:34:23.097  INFO 59360 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Hibernate: create table social_link (author_username varchar(255) not null, type varchar(255) not null, value varchar(255), primary key (author_username, type)) engine=MyISAM
2018-09-30 07:34:24.067  WARN 59360 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement

Strange thing is that I can execute SQL statement via the SQL workbench without a problem and everything works fine after that. Here is the entity that is responsible for that table:

@Entity
public class SocialLink {

    @EmbeddedId
    private SocialLinkKeyEmbeddable id = new SocialLinkKeyEmbeddable();
    private String value;
    @ManyToOne
    @MapsId("author_username")
    @JoinColumns({
            @JoinColumn(name="author_username", referencedColumnName="author_username")
    })
    private Author author;

    //Getters and setters
}

@Embeddable
public class SocialLinkKeyEmbeddable implements Serializable {

    @Column(name = "author_username")
    private String author_username;
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAuthor_username() {
        return author_username;
    }

    public void setAuthor_username(String author_username) {
        this.author_username = author_username;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}

public interface SocialLinkRepository extends CrudRepository<SocialLink, SocialLinkKeyEmbeddable> {
}
like image 929
Boris Pavlovski Avatar asked Sep 30 '18 04:09

Boris Pavlovski


1 Answers

The problem was with the length of the key. MySQL was pretending it is larger than 1000 bytes. It seems its a common problem with MyISAM storage engine. In order to fix it I added:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

to my applicaiton.properties and now the problem is fixed. Was using this post as reference #1071 - Specified key was too long; max key length is 1000 bytes

like image 188
Boris Pavlovski Avatar answered Oct 03 '22 23:10

Boris Pavlovski