我应该对手动实现的Spring Data存储库方法使用Java 8默认方法吗?
在使用新的Spring Data Evans版本时,能够使用java 8附带的一些好东西真是太好了。其中之一是接口中的默认实现。下面的存储库使用 QueryDSL 使查询类型安全。
我的问题是,在我写这篇文章之前,我使用了一个单独的接口的模式,然后是另一个类,在那个类中,我将得到电流。UserRepositoryCustom
findByLogin
UserRepositoryImpl
@PersistenceContext
EntityManager
当我没有课程时,我如何获得?这有可能吗?EntityManager
@Repository
public interface UserRepository extends JpaRepository<User, UUID> {
final QUser qUser = QUser.user;
// How do I get the entityManager since this is a interface, i cannot have any variables?
//@PersistenceContext
//EntityManager entityManager;
public default Optional<User> findByLogin(String login) {
JPAQuery query = new JPAQuery(entityManager);
User user = query
.from(qUser)
.where(
qUser.deleter.isNull(),
qUser.locked.isFalse(),
qUser.login.equalsIgnoreCase(login)
)
.singleResult(qUser);
return Optional.ofNullable(user);
}
}