JPA-如何将同一查询添加到存储库两次
我尝试多次使用同一个查询,并通过注释设置不同的选项。类似于:JPA-如何将同一查询添加到存储库两次,jpa,Jpa,我尝试多次使用同一个查询,并通过注释设置不同的选项。类似于: @EntityGraph(attributePaths = {"books"}) Optional<User> findById(long id); @EntityGraph(attributePaths = {"courses"}) Optional<User> findById(long id); Optional<A_Projection> fi
@EntityGraph(attributePaths = {"books"})
Optional<User> findById(long id);
@EntityGraph(attributePaths = {"courses"})
Optional<User> findById(long id);
Optional<A_Projection> findById(long id);
例如,对于投影情况,这是一种有效的语法:
Optional<A_Projection> findByIdByProjection(long id);
可选的findByIdByProjection(长id);
您可以使用entitygraph HJPA SpecificationExecutor
根据您的方法传递不同的entitygraph
@Repository
public interface UserRepository extends JpaSpecificationExecutor<User>, JpaRepository<User, Long>, EntityGraphJpaSpecificationExecutor<User> {
}
@存储库
公共接口用户存储库扩展了JpaSpecificationExecutor、JpaRepository、EntityGraphJpaSpecificationExecutor{
}
在服务类中,可以使用实体图调用findOne
List<User> users = userRepository.findOne(specification, new NamedEntityGraph(EntityGraphType.FETCH, "graphName"))
List users=userRepository.findOne(规范,新名称dentityGraph(EntityGraphType.FETCH,“graphName”))
我找到了我要找的东西。显然,JPA在派生查询中搜索find….By
。因此,您可以在这些条款之间添加任何内容:
Optional<User> findWithBooksById(long id); //Q1,Q2,Q3 prefixes help distinguish these methods
@EntityGraph(attributePaths = {"courses"})
Optional<User> findWithWhateverById(long id); //but they mess up the JPA syntax
Optional<A_Projection> findById(long id);
可选findWithBooksById(长id)//Q1、Q2、Q3前缀有助于区分这些方法
@EntityGraph(AttributePath={“课程”})
可选findWithWhateverById(长id)//但是他们弄乱了JPA语法
可选的findById(长id);
来自Spring文档:
find(或其他引入关键字)和By之间的任何文本都被视为描述性文本,除非使用其中一个结果限制关键字(如Distinct)在要创建的查询上设置Distinct标志或Top/First限制查询结果
Optional<User> findWithBooksById(long id); //Q1,Q2,Q3 prefixes help distinguish these methods
@EntityGraph(attributePaths = {"courses"})
Optional<User> findWithWhateverById(long id); //but they mess up the JPA syntax
Optional<A_Projection> findById(long id);