Skip to content
Advertisement

Jooq insert one2one and one2many relational data into 2 tables at once. How to deal with issues and rollback

F.e. model

How to insert it at once in jooq? How to deal with rollback if some query will fail? Is it has to be programated on my side? I can not find a word about such case in documentation 🙁

Should Id do it manually like:

  1. find author id in db by name
  2. if author not exist insert new author
  3. insert book with author id
  4. if book insertion fail then delete author if was inserted this time?

How to deal with it? Do You have any examples for that?

EDIT: I use h2 database

Advertisement

Answer

Simon has already provided you with feedback about the transactionality of your logic. Your 4 steps can be encoded as follows (assuming you’re using the code generator):

Instead of using the jOOQ transaction API, of course, you can use any other means of providing transactionality to your code, including Spring, Java EE, or JDBC directly.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement