Hi Paul, UpdatableRecord.store() will execute an UPDATE statement on records where jOOQ knows you fetched them before from the database. So, you'll typically need to hold a reference to the fetched record in order to do that, rather than create a new one. You can also set the ID value on the record, and call update() on the record, instead of store()
I hope this helps Lukas On Tue, Nov 14, 2023 at 1:52 PM Paul Aeschlimann <[email protected]> wrote: > Hi > > I try implementing CRUD operations with UpdateableRecords. As the primary > key field in my MySQL database I use the type SQLDataType.BINARY(16 > ).nullable(false) and for the type conversion to UUID in Java I use new > ByteArrayToUUIDConverter() which implements interface Converter<T, U>. > > When navigating to the edit form, I can fetch a specific record from the > database with this query: > TeachingMaterial teachingMaterial = this.create > .select() > .from(TEACHING_MATERIAL) > .where(TEACHING_MATERIAL.ID.eq(uuid)) > .fetchOneInto(TeachingMaterial.class); > > At the end, I fetch the record into a POJO (generated by jOOQ) in order I > can assign the attributes easily to the fields in my Thymeleaf template. > > In the controller method, I simply call the repository method which has > the following code: > public void save(TeachingMaterial teachingMaterial) { > TeachingMaterialRecord teachingMaterialRecord = this.create > .newRecord(TEACHING_MATERIAL); > > teachingMaterialRecord.setName(teachingMaterial.getName()); > teachingMaterialRecord.setIsActive(teachingMaterial.getIsActive()); > teachingMaterialRecord.setDeletedOn(teachingMaterial.getDeletedOn()); > teachingMaterialRecord.setAuthorId(teachingMaterial.getAuthorId()); > > teachingMaterialRecord.store(); > } > > Unfortunately, the store() function call here raises an error: > *Field 'id' doesn't have a default value* > > I assume it has to do with the remark "*When loading records from POJOs > <https://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos/>, > jOOQ will assume the record is a new record. It will hence attempt to > INSERT it.*" written here > <https://www.jooq.org/doc/latest/manual/sql-execution/crud-with-updatablerecords/simple-crud/> > . > > Isn't there a way I can use the store() function by creating/updating > records created from a POJO (or maybe later by a DTO which also holds > records from other tables from one-to-many/many-to-many relationships? > > -- > 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]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jooq-user/e93fc9f7-7435-444b-9d60-4099c094a49an%40googlegroups.com > <https://groups.google.com/d/msgid/jooq-user/e93fc9f7-7435-444b-9d60-4099c094a49an%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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]. To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAB4ELO6Ldv1Z1CA2dt2aY9Ezv9mAB_zyDFY_bMv928kZC_zO5Q%40mail.gmail.com.
