简体   繁体   English

动态 JPA 查询

[英]Dynamic JPA querys

I'm pretty new in JPA/Hibernate and spring boot, and I'd like to do dynamic querys depeding on the received params.我在 JPA/Hibernate 和 spring 引导中相当新,我想根据收到的参数进行动态查询。 I was following this approach https://www.baeldung.com/spring-data-jpa-query , but I have a doubt when it say: "Also, we need to make sure to include the Impl postfix in the class name. Spring will search the UserRepositoryCustom implementation as UserRepositoryCustomImpl. Since fragments are not repositories by themselves, Spring relies on this mechanism to find the fragment implementation."我遵循这种方法https://www.baeldung.com/spring-data-jpa-query ,但是当它说:“另外,我们需要确保在 class 名称中包含 Impl 后缀。 Spring 将作为 UserRepositoryCustomImpl 搜索 UserRepositoryCustom 实现。由于片段本身不是存储库,因此 Spring 依靠这种机制来查找片段实现。

What doest it means?这是什么意思? How can I include the Impl postfix in the class name?如何在 class 名称中包含 Impl 后缀?

Thanks谢谢

It means if you need any custom methods in your repository you need to declare an interface as follows这意味着如果您需要在存储库中使用任何自定义方法,您需要声明一个接口,如下所示

public interface UserRepositoryCustom {
List<User> findUserByEmails(Set<String> emails);
}

and now you need to provide the implementation of the interface as follows现在您需要提供接口的实现,如下所示

public class UserRepositoryCustomImpl implements UserRepositoryCustom {

@PersistenceContext
private EntityManager entityManager;

@Override
public List<User> findUserByEmails(Set<String> emails) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> user = query.from(User.class);

    Path<String> emailPath = user.get("email");

    List<Predicate> predicates = new ArrayList<>();
    for (String email : emails) {
        predicates.add(cb.like(emailPath, email));
    }
    query.select(user)
        .where(cb.or(predicates.toArray(new Predicate[predicates.size()])));

    return entityManager.createQuery(query)
        .getResultList();
}
}

spring will try to find the implementation of the interface with the Impl postfix and here it is UserRepositoryCustomImpl (interface name: UserRepositoryCustom + Impl(postfix)). spring 将尝试查找带有 Impl 后缀的接口的实现,这里是UserRepositoryCustomImpl (接口名称:UserRepositoryCustom + Impl(postfix))。 if you prefer another postfix you can add @EnableJpaRepositories( repositoryImplementationPostfix = "Impl2", ) on your configuration here Impl2 is your custom post fix.如果您更喜欢另一个后缀,您可以在此处的配置中添加@EnableJpaRepositories( repositoryImplementationPostfix = "Impl2", ) Impl2是您的自定义后缀。 So your implementation class name should be UserRepositoryCustomImpl2所以你的实现 class 名称应该是UserRepositoryCustomImpl2

Suppose you have an entity Person .假设您有一个实体Person You create a repository interface to that.您为此创建了一个存储库接口。

public interface PersonRepository extends JpaRepository<Person, String>{}

Then you need a custom logic.然后你需要一个自定义逻辑。 So you create a custom repository interface:因此,您创建了一个自定义存储库接口:

@NoRepositoryBean
public interface CustomPersonRepository { 
   [...]
}

And the implementation class with Impl postfix:以及带有Impl后缀的 class 的实现:

@Repository
public class CustomPersonRepositoryImpl implements CustomPersonRepository{
   
   @PersistenceContext
   private EntityManager entityManager;

   [...]
}

Now you add the custom interface to the original repository interface:现在您将自定义接口添加到原始存储库接口:

public interface PersonRepository extends JpaRepository<Person, String>,CustomPersonRepository{

}

When you @Autowired the PersonRepository interface, and call a method on the injected beans which belongs to the CustomPersonRepository , Spring will use the method of the CustomPersonRepositoryImpl .当您@Autowired PersonRepository接口,并在属于CustomPersonRepository的注入 bean 上调用方法时,Spring 将使用CustomPersonRepositoryImpl的方法。

So the name of the implementation class must be the same name as the custom interface + an Impl at the end.所以实现的名字 class 必须和自定义接口同名+最后一个Impl You can parameterize that postfix anyway with the repositoryImplementationPostfix property in the @EnableJpaRepositories annotation.无论如何,您都可以使用@EnableJpaRepositories注释中的repositoryImplementationPostfix属性参数化该后缀。

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

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