Hello,

You have a couple of options:

*Try inserting or updating on error:*

try {
    record.insert();
}
catch (DataAccessException e) {
    // Perhaps, check e's cause to be a
SQLIntegrityConstraintViolationException
    record.update();
}


Note that if there is an exception in an insert(), update(), or store()
operation, the internal changed flags won't be modified. This approach
obviously suffers from the usual possibility of creating race conditions if
multiple threads try to INSERT / UPDATE the same record.

*Running an explicit INSERT .. ON DUPLICATE KEY UPDATE statement:*

DSL.using(configuration)
   .insertInto(TABLE)

   .set(record)

   .onDuplicateKeyUpdate()

   .set(record)

   .execute();


Note the set(record) methods are convenience methods to copy all (changed)
values from your record into the statement. If you're not using MySQL or
MariaDB, the above statement will still work on all of those databases that
support the SQL standard MERGE statement.

Hope this helps
Lukas

2014-06-22 11:32 GMT+02:00 <[email protected]>:

> I would like to use "insert or update" on a record to either insert a new
> record if it does not exist or update if it does.
>
> I have following table:
>
> object_id  bigint not null,
> language   char(3) not null,
> content    text
>
> Primary key is: (object_id, language)
>
> If I use store() on a newly created and filled record, I'll get a
> violation exception if the primary key already exists. But I can't use
> update(), since I don't know if this record already exists.
>
> What is the preferred JOOQ way to do this?
>
> --
> 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.

Reply via email to