Spring Boot / Hibernate无法创建表

Bor*_*ski 2 mysql spring hibernate

当我尝试启动我的spring boot应用程序时,我得到此警告:

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
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我可以通过SQL工作台执行SQL语句而不会出现问题,然后一切正常。这是负责该表的实体:

@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> {
}
Run Code Online (Sandbox Code Playgroud)

Bor*_*ski 9

问题出在密钥的长度上。MySQL假装它大于1000个字节。MyISAM存储引擎似乎是一个普遍的问题。为了修复它,我添加了:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
Run Code Online (Sandbox Code Playgroud)

到我的applicaiton.properties,现在问题已解决。将此帖子用作参考 #1071-指定的密钥太长;最大密钥长度为1000个字节


Mar*_*nBG 7

spring.jpa.database-platform我遇到了由属性设置为 引起的类似问题MySQL5Dialect

将属性设置为MySQL8Dialect或完全删除它(启用自动配置)解决了问题。