Field 'id' doesn't have a default value

176 views
Skip to first unread message

Paul Aeschlimann

unread,
Nov 14, 2023, 7:52:18 AM11/14/23
to jOOQ User Group
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, jOOQ will assume the record is a new record. It will hence attempt to INSERT it." written here.

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?

Lukas Eder

unread,
Nov 14, 2023, 8:11:58 AM11/14/23
to jooq...@googlegroups.com
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

--
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 jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/e93fc9f7-7435-444b-9d60-4099c094a49an%40googlegroups.com.

Paul Aeschlimann

unread,
Dec 30, 2023, 6:14:06 AM12/30/23
to jOOQ User Group
Hi Lukas,

Sorry for my late answer. Thank you for your response.
I understand that I would have to fetch the record again before calling store() in my case. I work with Thymeleaf as a Template engine. Since this would end in executing two queries I opt for explicitly writing an update query.

Thanks again for your help.

Best regards
Paul
Reply all
Reply to author
Forward
0 new messages