Java 不同实体图的Spring数据JPA findAll
在SpringDataJPA存储库中,我需要指定多个执行相同操作的方法,例如findAll,但指定不同的@EntityGraph注释。目标是在不同的服务中使用优化的方法 是的 在Java中,我们不能让同一个方法多次签名,那么如何管理它呢 不使用JPQL是否可能 谢谢 Gabriele您可以使用EntityGraphJPASSpecificationExecutor根据您的方法传递不同的entitygraphJava 不同实体图的Spring数据JPA findAll,java,spring,spring-data-jpa,spring-data,entitygraph,Java,Spring,Spring Data Jpa,Spring Data,Entitygraph,在SpringDataJPA存储库中,我需要指定多个执行相同操作的方法,例如findAll,但指定不同的@EntityGraph注释。目标是在不同的服务中使用优化的方法 是的 在Java中,我们不能让同一个方法多次签名,那么如何管理它呢 不使用JPQL是否可能 谢谢 Gabriele您可以使用EntityGraphJPASSpecificationExecutor根据您的方法传递不同的entitygraph @Repository public interface UserRepository
@Repository
public interface UserRepository extends JpaSpecificationExecutor<User>, JpaRepository<User, Long>, EntityGraphJpaSpecificationExecutor<User> {
}
在您的服务类中,您可以调用find all with entity graph
List<User> users = userRepository.findAll(specification, new NamedEntityGraph(EntityGraphType.FETCH, "graphName"))
如上所述,您可以根据需要使用不同的实体图。您可以使用EntityGraph作为参数:太好了,我不知道您可以将实体图作为参数传递!谢谢谢谢你,你的回答对我很有用。谢谢你可以接受答案,让其他人知道sameI似乎找不到EntityGraphJPASSpecificationExecutor,你从哪里得到的?@La Hai,你需要为它添加依赖项https://mvnrepository.com/artifact/com.cosium.spring.data/spring-data-jpa-entity-graph/2.4.2
List<User> users = userRepository.findAll(specification, new NamedEntityGraph(EntityGraphType.FETCH, "graphName"))