Home >Database >Mysql Tutorial >java-hibernate如何配置mysql主键生成策略?

java-hibernate如何配置mysql主键生成策略?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 09:36:361281browse

mysqljavahibernate

在之前的使用过程中,是用的oracle,只需要用注解的方式在主键上加@GeneratedValue,新增操作时,就可以自动生成主键了。
但是现在这个项目转为用mysql,用@GeneratedValue就会报“java.sql.SQLException: Field '***' doesn't have a default value”

<code>@Id@GeneratedValue@Column(name = "rol_id")private Long rolId; // ID</code>

后来在网上搜了下,改成下面这样就不会报错了

<code>@Id@GeneratedValue(generator = "system-uuid")   @GenericGenerator(name = "system-uuid", strategy = "increment")@Column(name = "rol_id")private Long rolId; // ID</code>

这样设置之后,主键就相当于是自增型的了,我也不太清楚其中的道理
各位大侠,请问后面这种解决方式好吗?或者说要怎样完美的解决这个问题呢?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn