如何使用 JPA 注释注释 MYSQL 自动增量字段
直截了当,问题是将对象运算符保存到 MySQL 数据库中.在保存之前,我尝试从该表中进行选择,它可以正常工作,与 db 的连接也是如此.
Straight to the point, problem is saving the object Operator into MySQL DB. Prior to save, I try to select from this table and it works, so is connection to db.
这是我的 Operator 对象:
Here is my Operator object:
@Entity
public class Operator{
@Id
@GeneratedValue
private Long id;
private String username;
private String password;
private Integer active;
//Getters and setters...
}
为了保存,我使用 JPA EntityManager
的 persist
方法.
To save I use JPA EntityManager
’s persist
method.
这是一些日志:
Hibernate: insert into Operator (active, password, username, id) values (?, ?, ?, ?)
com.mysql.jdbc.JDBC4PreparedStatement@15724a0: insert into Operator (active,password, username, id) values (0, 'pass', 'user', ** NOT SPECIFIED **)
在我看来,问题是自动递增的配置,但我不知道在哪里.
The way I see it, problem is configuration with auto increment but I can't figure out where.
尝试了一些我在这里看到的技巧:Hibernate 不尊重 MySQL auto_increment 主键字段 但什么也没有其中的工作
Tried some tricks I've seen here: Hibernate not respecting MySQL auto_increment primary key field But nothing of that worked
如果需要任何其他配置文件,我会提供.
If any other configuration files needed I will provide them.
DDL:
CREATE TABLE `operator` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(40) NOT NULL,
`last_name` VARCHAR(40) NOT NULL,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(50) NOT NULL,
`active` INT(1) NOT NULL,
PRIMARY KEY (`id`)
)
推荐答案
要使用 MySQL AUTO_INCREMENT
列,您应该使用 IDENTITY
策略:
To use a MySQL AUTO_INCREMENT
column, you are supposed to use an IDENTITY
strategy:
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
在 MySQL 中使用 AUTO
会得到什么:
Which is what you'd get when using AUTO
with MySQL:
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
实际上相当于
@Id @GeneratedValue
private Long id;
换句话说,您的映射应该可以工作.但是 Hibernate 应该省略 SQL 插入语句中的 id
列,而事实并非如此.一定有某种不匹配的地方.
In other words, your mapping should work. But Hibernate should omit the id
column in the SQL insert statement, and it is not. There must be a kind of mismatch somewhere.
您是否在 Hibernate 配置中指定了 MySQL 方言(可能是 MySQL5InnoDBDialect
或 MySQL5Dialect
,具体取决于您使用的引擎)?
Did you specify a MySQL dialect in your Hibernate configuration (probably MySQL5InnoDBDialect
or MySQL5Dialect
depending on the engine you're using)?
另外,谁创建了这个表?可以显示对应的DDL吗?
Also, who created the table? Can you show the corresponding DDL?
跟进:我无法重现您的问题.使用 your 实体的代码和 your DDL,Hibernate 使用 MySQL 生成以下(预期的)SQL:
Follow-up: I can't reproduce your problem. Using the code of your entity and your DDL, Hibernate generates the following (expected) SQL with MySQL:
insert
into
Operator
(active, password, username)
values
(?, ?, ?)
请注意,如预期的那样,上述语句中缺少 id
列.
Note that the id
column is absent from the above statement, as expected.
总而言之,您的代码、表格定义和方言是正确且连贯的,它应该可以工作.如果它不适合您,则可能某些内容不同步(进行干净的构建,仔细检查构建目录等)或其他错误(检查日志是否有任何可疑内容).
To sum up, your code, the table definition and the dialect are correct and coherent, it should work. If it doesn't for you, maybe something is out of sync (do a clean build, double check the build directory, etc) or something else is just wrong (check the logs for anything suspicious).
关于方言,MySQL5Dialect
或MySQL5InnoDBDialect
唯一的区别在于后者添加了ENGINE=InnoDB
生成 DDL 时到表对象.使用其中一种不会更改生成的 SQL.
Regarding the dialect, the only difference between MySQL5Dialect
or MySQL5InnoDBDialect
is that the later adds ENGINE=InnoDB
to the table objects when generating the DDL. Using one or the other doesn't change the generated SQL.
相关文章