Hibernate-SpringBoot icon indicating copy to clipboard operation
Hibernate-SpringBoot copied to clipboard

HibernateSpringBootOneToManyBidirectional has issue with removeBooks method

Open swapy opened this issue 2 years ago • 1 comments

Hi,

Thanks for awesome book.

HibernateSpringBootOneToManyBidirectional has helper method removeBooks.

    @Transactional
    public void deleteAllBooksOfAuthor() {
        Author author = authorRepository.findByName("Joana Nimar");
        author.removeBooks(); // use removeBooks() helper    
    }

But if you see carefully, it calls delete for each book entity. Can we optimize it somehow to call like IN queries instead?

Also purpose of these helper methods is to call on managed entities or detached ones only?

swapy avatar Dec 26 '22 05:12 swapy

Hi,

I'm glad that you liked the book. Well, you probably need a helper as deleteViaBulkIn(). Could you check it in the book? The challenge is to keep the Persistence Context in sync. The helper methods from entities are for managed entities. When the entities are detached, you can perform any query as long as you know what you are doing. There is no Persistence Context present, so no sync is needed. I hope it helps :)

AnghelLeonard avatar Dec 26 '22 06:12 AnghelLeonard