java - 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的吗?
解决方案
你的意思是你想对 id 字段使用自动增量?如果是,您必须指定@GeneratedValue
to be的策略IDENTITY
:
@Entity
public class Test implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
}
推荐阅读
- java - 在 GET 请求上将 JSON 对象从 MongoDB 反序列化为 Java 对象
- react-native - React-Navigation 使用选项卡添加抽屉式导航
- html - 强制立即加载 SVG 文件和图标
- javascript - 圆近似,是具有 N 个角的正多边形
- matlab - 有没有办法防止 MATLAB 的 simple()/numden() 函数取消相等的分子/分母项?
- c# - 用括号和逗号替换字符串中的逗号(如果它们不存在)
- javascript - Javascript,将值传递给新添加的表格行/单元格
- html - 所有元素过滤但悬停
- firebase - updateEmail 向原始地址发送电子邮件的安全原因
- mysql - 从 MySQL 中的两个不同表中减去值