简体   繁体   中英

Can not execute delete operation by using deleteBy custom method in spring data repository

When I try to delete data from my service class using deleteBy() custom method in spring data repository it gives following error.

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

I tried with making method and service class @Transactional. Then autowire members get null. Those were initialize without @Transactional.

Load all the data that need to be deleted. getBYId(id); And use that collection to execute deleteAll() operation. That solve the issue.

您必须先加载对象

I was having the same issue, but after I annotated only the method with @Transactional , it worked.

@Repository()
interface UserRepository : CrudRepository<User, Long> {

    @Transactional
    fun deleteAllByCpf(cpf: String)
}

In my case, I'm using Springboot with Kotlin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM