Should I manually implement the spring data warehouse method using the Java 8 default method?

When using the new spring data Evans distribution, I'm glad to use some good things to use with Java 8 One is the default implementation in the interface The following repository uses querydsl to make query type safe

My problem is that before I write this article, I used a separate userrepositorycustom interface with the mode of findbylogin, and then another class userrepositoryimpl. In this class, I will have @ persistencecontext to get the current entitymanager

How do I get an entitymanager when I don't have a course? Is it even possible

@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);
    }
}

Solution

Only default methods can be used to delegate calls to other repository methods The default method (by definition) cannot access any state of the instance (because the interface does not) They can only delegate other interface methods or call static methods of other classes

In fact, using the custom implementation described in reference documentation is the right way This is a short version for reference (in case others want to know):

/**
 * Interface for methods you want to implement manually.
 */
interface UserRepositoryCustom {
  Optional<User> findByLogin(String login);
}

/**
 * Implementation of exactly these methods.
 */
class UserRepositoryImpl extends QueryDslRepositorySupport implements UserRepositoryCustom {

  private static final QUser USER = QUser.user;

  @Override
  public Optional<User> findByLogin(String login) {

    return Optional.ofNullable(
      from(USER).
      where(
        USER.deleter.isNull(),USER.locked.isFalse(),USER.login.equalsIgnoreCase(login)).
      singleResult(USER));
  }
}

/**
 * The main repository interface extending the custom one so that the manually
 * implemented methods get "pulled" into the API.
 */
public interface UserRepository extends UserRepositoryCustom,CrudRepository<User,Long> { … }

Note that naming conventions are important here (but can be customized if necessary) By extending querydslrepositorysupport, you can access the from (...) method so that you do not have to interact with the entitymanager yourself

Alternatively, you can have the userrepository implement querydslpredicateexecutor and pass predicates from outside the repository, but eventually you can use the client that needs to use querydsl (which may not be needed), and you have not obtained the optional wrapper type ootb

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>