如何使用 JPA 注释来注释 MYSQL 自动增量字段 [英] How to annotate MYSQL autoincrement field with JPA annotations

查看:24
本文介绍了如何使用 JPA 注释来注释 MYSQL 自动增量字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直截了当,问题是将对象 Operator 保存到 MySQL DB 中.在保存之前,我尝试从该表中进行选择并且它有效,与 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 EntityManagerpersist 方法.

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 方言(可能是 MySQL5InnoDBDialectMySQL5Dialect,具体取决于您使用的引擎)?

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?

跟进:我无法重现您的问题.使用你的实体和你的 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).

关于方言,MySQL5DialectMySQL5InnoDBDialect唯一区别在于后者增加了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.

这篇关于如何使用 JPA 注释来注释 MYSQL 自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆