問題描述
直截了當(dāng),問題是將對象運(yùn)算符保存到 MySQL 數(shù)據(jù)庫中.在保存之前,我嘗試從該表中進(jìn)行選擇,它可以正常工作,與 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 **)
在我看來,問題是自動(dòng)遞增的配置,但我不知道在哪里.
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
如果需要任何其他配置文件,我會(huì)提供.
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
列,您應(yīng)該使用 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
會(huì)得到什么:
Which is what you'd get when using AUTO
with MySQL:
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
實(shí)際上相當(dāng)于
@Id @GeneratedValue
private Long id;
換句話說,您的映射應(yīng)該可以工作.但是 Hibernate 應(yīng)該省略 SQL 插入語句中的 id
列,而事實(shí)并非如此.一定有某種不匹配的地方.
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)?
另外,誰創(chuàng)建了這個(gè)表?可以顯示對應(yīng)的DDL嗎?
Also, who created the table? Can you show the corresponding DDL?
跟進(jìn):我無法重現(xiàn)您的問題.使用 your 實(shí)體的代碼和 your DDL,Hibernate 使用 MySQL 生成以下(預(yù)期的)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
(?, ?, ?)
請注意,如預(yù)期的那樣,上述語句中缺少 id
列.
Note that the id
column is absent from the above statement, as expected.
總而言之,您的代碼、表格定義和方言是正確且連貫的,它應(yīng)該可以工作.如果它不適合您,則可能某些內(nèi)容不同步(進(jìn)行干凈的構(gòu)建,仔細(xì)檢查構(gòu)建目錄等)或其他錯(cuò)誤(檢查日志是否有任何可疑內(nèi)容).
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).
關(guān)于方言,MySQL5Dialect
或MySQL5InnoDBDialect
唯一的區(qū)別在于后者添加了ENGINE=InnoDB
生成 DDL 時(shí)到表對象.使用其中一種不會(huì)更改生成的 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.
這篇關(guān)于如何使用 JPA 注釋注釋 MYSQL 自動(dòng)增量字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!