简体   繁体   English

在JOOQ中插入MySQL的返回查询

[英]Insert Returning Query For MySQL in JOOQ

am trying to use the following code to get the auto generated id . 我试图使用以下代码来获取自动生成的ID。 My back end is MySQL. 我的后端是MySQL。 Code looks like this 代码看起来像这样

Record record = create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME,      
       CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
       .values("Charlotte", "Roche","Charlotte Roche")
       .returning(CANDIDATE.ID)
       .fetchOne();

System.out.println(record.getValue(CANDIDATE.ID));

I am getting NullPointerException. 我收到了NullPointerException。 I took a look at http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html . 我看了http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html It says 它说

Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as "generated key". Derby,H2,Ingres, MySQL, SQL Server仅允许将IDENTITY列值检索为“生成的密钥”。 If other fields are requested, a second statement is issued. 如果请求其他字段,则发出第二个语句。 Client code must assure transactional integrity between the two statements. 客户端代码必须确保两个语句之间的事务完整性。

As per my understanding in Mysql auto_increment works as IDENTITY. 根据我在Mysql中的理解,auto_increment可以作为IDENTITY使用。 Can anybody please throw some light on how to achieve this for MySQL 请问有谁可以说明如何为MySQL实现这一目标

I have taken a look at this SO Question on a similar topic and tried following 我已经看过关于类似主题的SO问题,并尝试了以下内容

Result<?>  record =
            create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME, CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
                  .values("Charlotte", "Roche","Charlotte Roche")
                  .returning(CANDIDATE.ID)
                  .fetch();

            System.out.println(record.size());

Though it inserts record in the backend but it prints the record.size() as zero 虽然它在后端插入记录但它将record.size()打印为零

I'm know that I'm late for the party. 我知道我迟到了。 But I hope I can help someone with similar problem, 但我希望我能帮助有类似问题的人,

Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as " generated key ". Derby,H2,Ingres,MySQL,SQL Server仅允许将IDENTITY列值检索为“ 生成的密钥 ”。 If other fields are requested, a second statement is issued. 如果请求其他字段,则发出第二个语句。 Client code must assure transactional integrity between the two statements. 客户端代码必须确保两个语句之间的事务完整性。

The words "generated key" is the problem. "generated key"这个词就是问题所在。 You can check if your table id is AUTO_INCREMENT or not by using SHOW CREATE TABLE $table_name . 您可以使用SHOW CREATE TABLE $table_name检查您的表ID是否为AUTO_INCREMENT I think it is not. 我认为不是。

P/s: I'm using MySQL P / s:我正在使用MySQL

Just did a test inserting a record and retrieving the generated id from within a Spring service without any problem. 刚刚做了一个测试,插入一条记录并从Spring服务中检索生成的id没有任何问题。

So yes, auto_increment in MySQL works as IDENTITY with jOOQ. 所以,是的,MySQL中的auto_increment与jOOQ一起作为IDENTITY。

The MySQL table looks like this: MySQL表如下所示:

CREATE TABLE persons (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `first_name` varchar(64) NOT NULL,
    `last_name` varchar(64) NOT NULL,
    primary key(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and the service like this: 和这样的服务:

public Result<PersonsRecord> insertPerson(String firstName, String lastName) {
    Result<PersonsRecord> result =
        dsl
            .insertInto(
                    PERSONS,
                    PERSONS.FIRST_NAME,
                    PERSONS.LAST_NAME)
            .values(
                    firstName,
                    lastName)
            .returning(PERSONS.ID)
            .fetch();
    logger.debug("Person ID: " + result.getValue(0, PERSONS.ID));
    return result;
}

The generated id is available straight away after executing the insert: 执行插入后,生成的id可立即使用:

Person ID: 4 

Maybe there is a problem with transaction. 也许交易存在问题。 Insert might not yet persisted those values in database, so nothing is fetched. Insert可能尚未在数据库中保留这些值,因此不会提取任何内容。

Also I think that IDENTITY in case of MySQL is not made by AUTO_INCREMENT but PRIMARY KEY (...) https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html 另外我认为在MySQL的情况下,IDENTITY不是由AUTO_INCREMENT制作的,而是PRIMARY KEY(...) https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM