gpt4 book ai didi

spring - JpaRepository中的自定义删除方法

转载 作者:行者123 更新时间:2023-12-02 20:12:44 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以重写某些 JpaRepository 的删除方法,而不必重写其余方法。

目前我有类似的东西

public interface UserRepo extends JpaRepository<User, Long>
{
findUserById(long id);

findUserByEmail(String email);

// etc...
}

我想重写 CrudRepository 中的 delete(T Entity) 方法。为此,我尝试实现 UserRepo 但随后我必须实现所有 findByX 并且还没有真正找到有关如何正确执行此操作的任何信息。

是否有任何注释可以添加到实体类中的函数,以便它在调用UserRepo.delete(myUser)时运行?

提前致谢!

最佳答案

不确定我对你的理解是否足够清楚,但让我们尝试一下:

... I have to implement all the findByX ...

你不这样做,如果你在接口(interface)中使用合适的对流命名方法,spring将生成JPQL片段,请查看thisthis文章

... Is there any annotation to add to a function in the Entity class so it runs when you call UserRepo.delete(myUser)? ...

您可以在实体类中的方法上使用@PreRemove/@PostRemove注释:

@PreRemove / @PostRemove
public void someMethod() { ... }

关于spring - JpaRepository中的自定义删除方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53206163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com