mysql - Spring boot/Hibernate 无法创建表

标签 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

奇怪的是,我可以毫无问题地通过 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> {
}

最佳答案

问题出在 key 的长度上。 MySQL 假装它大于 1000 字节。这似乎是 MyISAM 存储引擎的常见问题。 为了修复它,我添加了:

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

到我的 applicaiton.properties,现在问题已解决。 正在使用这篇文章作为引用 #1071 - Specified key was too long; max key length is 1000 bytes

关于mysql - Spring boot/Hibernate 无法创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574685/

相关文章:

java - 您如何在 spring-security 中注销所有已登录的用户?

java - 如何从域对象创建 JSON View ?

Spring框架+NoSQL

hibernate - 如何在自定义 RevisionEntity 中使用 "org.hibernate.envers.default_schema"

mysql - 批处理文件 "Pressing"自动进入

MySQL 查询花费太长时间才能完成

Spring、spring Batch、hibernate 和 JUnit 以及多个集成测试的初始化

java - @CollectionTable 和 @ElementCollection 用法

php - 一对多与mysql中的特定数字

mysql - 如何在cakephp中编写Mysql DATE_FORMAT?