I think I'm missing something major about how UpdatableRecord.update()
works.
Example code that doesn't work:
MailKeywordRealAddressRecord linkRecord = dsl.select().
from(MAIL_KEYWORD_REAL_ADDRESS).
where(MAIL_KEYWORD_REAL_ADDRESS.MAIL_KEYWORD_ID.eq(mailKeyword.getId())).
fetchOneInto(MailKeywordRealAddressRecord.class);
if( linkRecord == null ){
log.debug("no link record found for our mailKeyword");
linkRecord = dsl.newRecord(MAIL_KEYWORD_REAL_ADDRESS);
linkRecord.setMailKeywordId(mailKeyword.getId());
linkRecord.setRealAddressId(realAddress.getId());
linkRecord.insert();
}
else {
// 1 - will geta "DataChangedException" because it tries to do a
// "select ... for udpate" with the *new* realAddress id
log.debug("updating old linkRecord from: {}", linkRecord );
linkRecord.setRealAddressId(realAddress.getId());
linkRecord.update();
// 2 - working
// log.debug("updating old linkRecord from: {}", linkRecord );
// linkRecord.delete();
// linkRecord.setRealAddressId(realAddress.getId());
// linkRecord.insert();
}
This will result in an exception:
DataChangedException: Database record no longer exists
And if I look at the SQL, JOOQ is issuing a select for update
SQL statement using the new value of RealAddressId.
If I comment out the (1) block and use (2) - JOOQ appears to do what I want, it deletes the old record and updates the new one.
The table underneath MailKeywordRealAddressRecord is an ordinary many-to-many link table (two columns, with both being declared as the composite primary key).
Thinking about it... - is that the problem? That I'm updating a primary key column? I'm happy enough to stick with the delete/insert logic (or I could refactor to a direct SQL statement) - just want to figure out what's going on.
Database is Postgres, JOOQ version is 3.10.1.
Settings.executeWithOptimisticLockingIncludeUnversioned
feature? What was your expectation when you turned that feature on? – Lukas Eder