Hi Serban, We're aware of the issue. Improving the situation is on the roadmap: https://github.com/jOOQ/jOOQ/issues/7444
The approach you've chosen is perfectly fine as the code generator is doing the same thing. The "ugly" part here is that being internal API, there's risk of your code breaking between minor releases (and rarely between patch releases) as there is no backwards compatibility guarantee for internal API. I hope this helps, Lukas 2018-04-30 13:17 GMT+02:00 <[email protected]>: > Hi, > > I would like to create programmatically some tables (without using the > code generator) and then execute CRUD operations on them. > > My initial approach was (see the whole code here > <https://gist.github.com/siordache/c89777eb69756b2b1295840625846cbf#file-jooqupsertv1-java> > ): > > final static Table<Record> PRODUCT = table(name("PRODUCT")); > final static Field<String> NAME = field(name("NAME"), String.class); > final static Field<String> COLOR = field(name("COLOR"), String.class); > > public void create() { > dsl.createTable(PRODUCT) > .column(NAME, NAME.getDataType().nullable(false)) > .column(COLOR, COLOR.getDataType().nullable(false)) > .constraints( > constraint(name("PK_PRODUCT")).primaryKey(NAME) > ) > .execute(); > } > > int upsert(String name, String color) { > return dsl.insertInto(PRODUCT) > .columns(NAME, COLOR) > .values(name, color) > .onDuplicateKeyUpdate() > .set(COLOR, color) > .execute(); > } > > Calling the upsert() method results in an IllegalStateException: The ON > DUPLICATE KEY IGNORE/UPDATE clause cannot be emulated when inserting into > non-updatable tables : "PRODUCT". > > The PRODUCT table is not updatable because DSL.table() creates a TableImpl > instance and TableInstance.getPrimaryKey() always returns null. > > I was able to make the code work by subclassing TableImpl and overriding > getPrimaryKey() (see the whole code here > <https://gist.github.com/siordache/c89777eb69756b2b1295840625846cbf#file-jooqupsertv2-java> > ): > > private static class UpsertTable extends TableImpl<Record> { > private UniqueKey<Record> primaryKey; > > protected UpsertTable(Name name) { > super(name); > } > > public void setPrimaryKey(String keyName, TableField<Record,?> > field) { > this.primaryKey = Internal.createUniqueKey(this, keyName, > field); > } > > @Override > public UniqueKey<Record> getPrimaryKey() { > return primaryKey; > } > } > > > However, I feel uncomfortable using classes that are marked "for JOOQ > INTERNAL USE only". Is there a better way to create updatable tables > programmatically? > > Thanks, > Serban > > -- > You received this message because you are subscribed to the Google Groups > "jOOQ User Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "jOOQ User Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
