简体   繁体   English

如何在 Spring Data JPA 中制作自定义方法

[英]How to make a custom method in Spring Data JPA

Currently, I'm learning about Spring Data JPA, and I found that I need to create a custom interface to make a custom methods.目前,我正在学习 Spring Data JPA,我发现我需要创建一个自定义接口来制作自定义方法。 I wonder if someone can explain to me why this is so.我想知道是否有人可以向我解释为什么会这样。 Let's say I have entity User with fields: userId and name .假设我有带有字段的实体UseruserIdname Also, I have UserRepository :另外,我有UserRepository

public interface UserRepository extends JpaRepository<User, Long> {...}

I found that I need to make new interface - UserRepositoryCustom where I should make custom abstract methods.我发现我需要创建新的接口UserRepositoryCustom ,我应该在其中创建自定义抽象方法。 Also, after that, I found that I need to make class UserRepositoryImpl which implements UserRepositoryCustom .另外,在那之后,我发现我需要创建实现UserRepositoryCustom UserRepositoryImpl Let's take a look at the code I made:让我们看一下我制作的代码:

@Repository
public class UserRepositoryImpl implements UserRepositoryCustom{

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<User> findUserByName(String name) {
        Query query = entityManager.createNativeQuery("SELECT em.* FROM User em " +
                "WHERE em.name = ?1", User.class);
        query.setParameter(1, name);
        return query.getResultList();
    }
}

Can you please explain why I need UserRepositoryCustom and UserRepositoryImpl in foreground, And why do I need EntityManager and @PersistenceContext above it?你能解释一下为什么我在前台需要UserRepositoryCustomUserRepositoryImpl ,为什么我需要EntityManager@PersistenceContext在它上面? Every piece of information is of great importance to me, because that way I will be able to extract what is most important for me, which is to understand.每一条信息对我来说都非常重要,因为这样我就能够提取对我来说最重要的东西,那就是理解。

I found this code, which is working fine, online, but I need to understand it.我在网上找到了这个运行良好的代码,但我需要理解它。

You don't need to do this.不需要这样做。 The direct equivalent would be a method in your UserRepository interface defined as List<User> findByName(String name) .直接等效的方法是UserRepository接口中定义为List<User> findByName(String name)的方法。 You can also use JPA named queries, the @Query annotation, etc.您还可以使用 JPA 命名查询、 @Query注释等。

The usage of additional interfaces and implementation classes is intended for advanced use cases that are not (easily) possible in the queries Spring Data JPA generates for you based on method names, annotations, etc. It is not intended for easy use cases like the query you show.附加接口和实现类的使用旨在用于 Spring Data JPA 根据方法名称、注释等为您生成的查询中不可能(容易)实现的高级用例。它不适用于像查询这样的简单用例你展示。

And the reason why, is because that is how Spring Data JPA is designed.之所以如此,是因为 Spring Data JPA 就是这样设计的。 I highly recommend you read the full Spring Data JPA documentation .我强烈建议您阅读完整的Spring Data JPA 文档 You'll notice that the solution in your question is just a minor part of the documentation, which makes clear that this is an escape hatch, not the primary way of using Spring Data JPA.您会注意到您的问题中的解决方案只是文档的一小部分,这清楚地表明这是一个逃生舱口,而不是使用 Spring Data JPA 的主要方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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