java - How to insertInto() without setting values and return the newly created ID? -


how can insert record without specifying values ans return newly created id? if this:

private long createoffer() {      return this.ctx.insertinto(storeoffer.store_offer, storeoffer.store_offer.id)             .returning(storeoffer.store_offer.id)             .fetchone().getvalue(storeoffer.store_offer.id); } 

i getting java.lang.nullpointerexception. if set null not-null-constraint gets triggered..

private long createoffer() {      return this.ctx.insertinto(storeoffer.store_offer, storeoffer.store_offer.id)             .values((long)null)             .returning(storeoffer.store_offer.id)             .fetchone().getvalue(storeoffer.store_offer.id); } 

i tried this:

private long createoffer() {     storeofferrecord storeofferrecord = this.ctx.newrecord(storeoffer.store_offer);     storeofferrecord.store();     return storeofferrecord.getid(); } 

but id null.


the table store_offer looking this:

create table store_offer (      -- primary key      id bigserial primary key ); 

using insert .. default values

jooq supports sql standard default values clause on insert statements use-case:

return this.ctx.insertinto(storeoffer.store_offer)                .defaultvalues()                .returning(storeoffer.store_offer.id)                .fetchone().getvalue(storeoffer.store_offer.id); 

see also: http://www.jooq.org/doc/latest/manual/sql-building/sql-statements/insert-statement/insert-default-values/

why got nullpointerexception

you able write following query because of api flaw (#3771). following not supposed compile:

return this.ctx.insertinto(storeoffer.store_offer, storeoffer.store_offer.id)                .returning(storeoffer.store_offer.id)                .fetchone().getvalue(storeoffer.store_offer.id); 

why record.store() didn't work:

jooq records have internal per-column changed flag. there no record-level changed flag indicate whole record needs sent database, without changes. specifically, wouldn't make sense if update statement need issued.


Comments

Popular posts from this blog

multithreading - Exception in Application constructor -

React Native allow user to reorder elements in a scrollview list -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -