首页 > 解决方案 > Spring Hibernate mysql:字段'id'没有默认值

问题描述

我知道之前有人问过这个问题,但无法解决问题。我想通过@Entitiy 类生成一个表。

@Entity
public class Test implements Serializable{
    @Id
    @GeneratedValue long id;

.
.
.
}

在我的应用程序属性中,我得到了:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true

我正在运行 mysql 5.7。问题是创建了表并且 id 被标记为主键。但是 Default 是 NULL 并且 Extra (我期望自动增量)是空的。

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | bigint(20)   | NO   | PRI | NULL    |       |
+----------+--------------+------+-----+---------+-------+

有什么我遗漏的或者是mittsonfigured的吗?

标签: javamysqlspringjpa

解决方案


你的意思是你想对 id 字段使用自动增量?如果是,您必须指定@GeneratedValueto be的策略IDENTITY

@Entity
public class Test implements Serializable {

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

}

推荐阅读