我正在研究Spring Data JPA.考虑下面的示例,我将默认使用所有crud和finder功能,如果我想自定义查找器,那么也可以在界面本身轻松完成.
@Transactional(readOnly = true)
public interface AccountRepository extends JpaRepository<Account, Long> {
@Query("<JPQ statement here>")
List<Account> findByCustomer(Customer customer);
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何为上述AccountRepository添加一个完整的自定义方法及其实现?由于它的接口我无法在那里实现该方法.
我已在 Spring Data JpaRespository 上添加了一个具有单个自定义方法的自定义接口,如该问题的答案中所述;
但是现在我收到以下错误;
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property customMethod found for type Account!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
Run Code Online (Sandbox Code Playgroud)
这似乎是因为 Spring Data 试图生成“customMethod”的查询,认为它是“Account”的属性。
如何停止给定方法的自动查询生成?!
更新我的代码具体如下;
public interface CacheInvalidatingRepo<T> {
public void invalidateCache(T obj);
}
@Component
public class CacheInvalidatingRepoImpl<T> implements CacheInvalidatingRepo<T> {
@Override
public void invalidateCache(T obj) {
// kill the entity manager cache
}
}
public interface VerificationRepo extends JpaRepository<Verification, BigInteger>, JpaSpecificationExecutor<Verification>, CacheInvalidatingRepo<Verification> {
}
Run Code Online (Sandbox Code Playgroud)
结果如下; …
我创建了自定义存储库并想要创建自定义本机查询:
public Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable)
Run Code Online (Sandbox Code Playgroud)
我如何适应 Pageable 对象并获取 Page<> 对象的结果?