如何用注释JPA注解MYSQL自动增量场 [英] How to annotate MYSQL autoincrement field with JPA annotations

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

问题描述

开门见山,问题是储蓄的对象为运营商的MySQL数据库。
在此之前保存,我尝试从该表中选择和它的作品,所以连接数据库。

下面是我的操作对象:

  @Entity
公共类操作{   @ID
   @GeneratedValue
   私人长期身份证;   私人字符串用户名;   私人字符串密码;
   私人整数活跃;   // getter和setter方法​​...
}

要救我使用JPA 的EntityManager 坚持方法。

下面是一些日志:

 休眠:(?,?,?,),插入运算符(主动,密码,用户名,ID)值
com.mysql.jdbc.JDBC4$p$pparedStatement@15724a0:插入运算符(主动,密码,用户名,ID)值(0,'通','用户',** NOT SPECIFIED **)

我看到它的方式,问题是自动递增的配置,但我想不通的地方。

试用了一些窍门,我在这里看到:
<一href=\"http://stackoverflow.com/questions/582526/hibernate-not-respecting-mysql-auto-increment-primary-key-field\">Hibernate不尊重MySQL的AUTO_INCREMENT主键字段但没有那工作

如果需要任何其他配置文件,我会向他们提供。

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 策略:

  @Id @GeneratedValue(策略= GenerationType.IDENTITY)
私人长期身份证;

这是使用时, AUTO 与MySQL你会得到什么:

  @Id @GeneratedValue(策略= GenerationType.AUTO)
私人长期身份证;

这实际上是相当于

  @Id @GeneratedValue
私人长期身份证;

在换句话说,你的映射应该工作。但是Hibernate的应该省略SQL INSERT语句的 ID 列,事实并非如此。必须有一种不匹配的地方的。

你在Hibernate配置中指定一个MySQL方言(大概 MySQL5InnoDBDialect MySQL5Dialect 根据发动机你重新使用)?

此外,谁创建的表?您可以显示相应的DDL?

追问:我无法重现你的问题。使用的code的的实体的的DDL,Hibernate会与MySQL以下(预期)SQL:

 插入

    操作者
    (活跃,密码,用户名)

    (?,?,?)

请注意, ID 列从上面的语句不存在,符合市场预期。

要总结一下,你的code,表定义和方言是正确的,连贯的,它应该工作。如果没有你,或许真的是不同步(做一个干净的构建,仔细检查build目录等),或别的东西是错误的(检查任何可疑的日志)。

关于方言的的只有的区别 MySQL5Dialect MySQL5InnoDBDialect 的是,后来增加了 ENGINE = InnoDB的生成DDL时,该表的对象。使用一种或另一种不改变生成的SQL。

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.

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...
}

To save I use JPA EntityManager’s persist method.

Here is some log:

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.

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`)
)

解决方案

To use a MySQL AUTO_INCREMENT column, you are supposed to use an IDENTITY strategy:

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

Which is what you'd get when using AUTO with MySQL:

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

Which is actually equivalent to

@Id @GeneratedValue
private Long 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.

Did you specify a MySQL dialect in your Hibernate configuration (probably MySQL5InnoDBDialect or MySQL5Dialect depending on the engine you're using)?

Also, who created the table? Can you show the corresponding DDL?

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
    (?, ?, ?)

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).

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天全站免登陆