简体   繁体   中英

How do I use Jooq insert ... returning in MySQL without code generation?

I've been trying to use insert...returning in MySQL with the DSL-based table definition (I'm not using the code generation) and my returned record is always null. Based on reading, I need to specify the identify column in the table definition, but I have no idea how!

Record recordKey = create.insertInto(table("modulerecords"), 
                                    field("id"), 
                                    field("module_id"),
                                    field("created_date"), 
                                    field("created_by"), 
                                    field("state"), 
                                    field("tag_id"),
                                    field("start_time",Timestamp.class), 
                                    field("kill_time", Timestamp.class), 
                                    field("feed_guid")
                                    )
                            .values(null, moduleId, currentTimestamp(), 
                                    userId, state, tagId, 
                                    new Timestamp(startTime), 
                                    new Timestamp(killTime), feedGuid)
                            .returning(field("id"))
                            .fetchOne();

The field "id" is auto_increment primary key in the database, but recordKey is always null.

It is highly recommended you use the code generator to provide all the meta information to the DSL API. You can, of course, not use the code generator and still use the internal APIs that the code generator would otherwise use. Instead of creaating your table and field references using the plain SQL API , you'd have to create a TableImpl subclass and override / implement all the relevant methods.

Or, you just use the code generator.

As of jOOQ 3.14, this is possible by specifying the field's datatype as being an identity, which can be done using SQLDataType.INTEGER.identity(true) .

So for example, if you had a table with an auto-generating integer id and a string name, you would call:

int id = DSL.using(connection, MYSQL_5_7)
            .insertInto(
                table("myTable"),
                field("name", String.class))
            .values("John Smith")
            .returning(field("id", SQLDataType.INTEGER.identity(true)))
            .fetchAny(field("id", Integer.class))

So for your example, you would do

Record recordKey = create.insertInto(table("modulerecords"), 
                                    field("id"), 
                                    field("module_id"),
                                    field("created_date"), 
                                    field("created_by"), 
                                    field("state"), 
                                    field("tag_id"),
                                    field("start_time",Timestamp.class), 
                                    field("kill_time", Timestamp.class), 
                                    field("feed_guid")
                                    )
                            .values(null, moduleId, currentTimestamp(), 
                                    userId, state, tagId, 
                                    new Timestamp(startTime), 
                                    new Timestamp(killTime), feedGuid)
                            .returning(field("id", SQLDataType.INTEGER.identity(true)))
                            .fetchOne();

Seethis Github comment for more background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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