0

In my code when I delete a row for a certain object, that does not happen immediately. In this particular case the row being deleted has a reference in another table, and given the fact that there is a foreign key, that results in an error, and an exception should be thrown. However, that does not happen when it's supposed to be deleted (calling deleteById() in the repository). The exception is thrown when I'm updating another row, so it seems like it for some reason was put in queue.

Anyone who knows what exactly is going on here?

rune
  • 79
  • 10
  • Edit your question to include the exception that is thrown. Please also add a snippet of the code being called to do the deletion. – Freiheit Jan 30 '19 at 14:03
  • Are your JPA objects correctly annotated for the foreign key relationship? – Freiheit Jan 30 '19 at 14:03

1 Answers1

0

You have to call flush() on the repository to have the SQL statements executed.

By default SQL statements are executed on transaction commit.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • adding @Transactional to the service function that called the repository function, made the query execute at once. The repository interface has the annotation @Transactional(readOnly = true), but it seems like that has got nothing to do with this at all. – rune Jan 31 '19 at 07:24
  • There is a good explanation of readOnly https://stackoverflow.com/questions/1614139/spring-transactional-read-only-propagation – Simon Martinelli Jan 31 '19 at 07:58