Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 482 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring JpaRepostory delete vs deleteInBatch

#1
What is the difference between `delete(...)` and `deleteInBatch(...)` methods in JpaRepostory in Spring ? The second one "deletes items in one SQL statement", but what does it mean from the application/database perspective ? Why exists two different methods with the similar results and when it is better to use one or other ?

**EDIT:**
The same applies also for `deleteAll()` and `deleteAllInBatch()` ...
Reply

#2
The `delete` method is going to delete your entity in one operation. The `deleteInBatch` is going to batch several delete-statements and delete them as 1 operation.

If you need a lot of delete operations the batch-deletion might be faster.
Reply

#3
`deleteInBatch(...)` in the log would look like this:
```DELETE FROM table_name WHERE (((((((? = id) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id))```

That might leads to a problem if there are a large amount of data to be deleted, which reaches maximum size of the SQL server query:

[To see links please register here]


Reply

#4
The answers here are not complete!

First off, let's check [the documentation][1]!

void deleteInBatch(Iterable<T> entities)
Deletes the given entities in a batch which means it will create a single Query.

So the "delete[All]InBatch" methods will use a JPA Batch delete, like "DELETE FROM table [WHERE ...]". That may be WAY more efficient, but it has some caveats:

* This will not call any JPA/Hibernate lifecycle hooks you might have (@PreDelete)
* It will NOT CASCADE to other entities
* You have to clear your persistence context or just assume it is invalidated.

That's because JPA will issue a bulk DELETE statement to the database, bypassing the cache etc. and thus can't know which entities were affected.

[See Hibernate Docs][2]

[The actual code][3] [in Spring Data JPA][4]

And while I can't find a specific article here, I'd recommend everything [Vlad Mihalcea][5] has written, to gain a deeper understanding of JPA.

TLDR: The "inBatch" methods use bulk DELETE statements, which can be drastically faster, but have some caveats bc. they bypass the JPA cache. You should really know how they work and when to use them to benefit.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

Reply

#5
Just do add curious information.

You can't create your custom delete using 'batch' on the method name and wait for spring data to resolve it, for example, you can't do this:

```
void deleteByYourAttributeInBatch(Iterable<YourObject> object);
```

Do you need to do something like this:

```
@Modifying
@Transactional
@Query("DELETE FROM YourObject qr WHERE o.yourAtribute IN (:object)")
void deleteByYourAttributeInBatch(Iterable<YourObject> o);
```

Maybe it's an issue to spring-data ;)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through