• <small id='aNcJv'></small><noframes id='aNcJv'>

    1. <i id='aNcJv'><tr id='aNcJv'><dt id='aNcJv'><q id='aNcJv'><span id='aNcJv'><b id='aNcJv'><form id='aNcJv'><ins id='aNcJv'></ins><ul id='aNcJv'></ul><sub id='aNcJv'></sub></form><legend id='aNcJv'></legend><bdo id='aNcJv'><pre id='aNcJv'><center id='aNcJv'></center></pre></bdo></b><th id='aNcJv'></th></span></q></dt></tr></i><div id='aNcJv'><tfoot id='aNcJv'></tfoot><dl id='aNcJv'><fieldset id='aNcJv'></fieldset></dl></div>

    2. <legend id='aNcJv'><style id='aNcJv'><dir id='aNcJv'><q id='aNcJv'></q></dir></style></legend>
          <bdo id='aNcJv'></bdo><ul id='aNcJv'></ul>
      1. <tfoot id='aNcJv'></tfoot>

        如何使用 JPA 注释注释 MYSQL 自动增量字段

        时间:2023-07-26

      2. <i id='78j61'><tr id='78j61'><dt id='78j61'><q id='78j61'><span id='78j61'><b id='78j61'><form id='78j61'><ins id='78j61'></ins><ul id='78j61'></ul><sub id='78j61'></sub></form><legend id='78j61'></legend><bdo id='78j61'><pre id='78j61'><center id='78j61'></center></pre></bdo></b><th id='78j61'></th></span></q></dt></tr></i><div id='78j61'><tfoot id='78j61'></tfoot><dl id='78j61'><fieldset id='78j61'></fieldset></dl></div>
          <tfoot id='78j61'></tfoot>
            • <bdo id='78j61'></bdo><ul id='78j61'></ul>

              1. <small id='78j61'></small><noframes id='78j61'>

                  <tbody id='78j61'></tbody>
                <legend id='78j61'><style id='78j61'><dir id='78j61'><q id='78j61'></q></dir></style></legend>

                • 本文介绍了如何使用 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 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?

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

                  关于方言,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 自动增量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 AspectJ 模拟接口和方法的注释继承 下一篇:如何使用@Value Spring Annotation 注入 Map?

                  相关文章

                  最新文章

                    <bdo id='ugIdy'></bdo><ul id='ugIdy'></ul>
                  1. <tfoot id='ugIdy'></tfoot>
                  2. <small id='ugIdy'></small><noframes id='ugIdy'>

                      <i id='ugIdy'><tr id='ugIdy'><dt id='ugIdy'><q id='ugIdy'><span id='ugIdy'><b id='ugIdy'><form id='ugIdy'><ins id='ugIdy'></ins><ul id='ugIdy'></ul><sub id='ugIdy'></sub></form><legend id='ugIdy'></legend><bdo id='ugIdy'><pre id='ugIdy'><center id='ugIdy'></center></pre></bdo></b><th id='ugIdy'></th></span></q></dt></tr></i><div id='ugIdy'><tfoot id='ugIdy'></tfoot><dl id='ugIdy'><fieldset id='ugIdy'></fieldset></dl></div>
                      <legend id='ugIdy'><style id='ugIdy'><dir id='ugIdy'><q id='ugIdy'></q></dir></style></legend>