329982 (1) [Avatar] Offline
#1
Hello,

After adding the Security components and the custom security config, I am getting the following error at compile time (using gradle build):

Task :compileJava
/Users/martinwunderlich/Dev/Spring/readinglist/src/main/java/com/myname/spring/readinglist/SecurityConfig.java:38: error: method findOne in interface QueryByExampleExecutor<T> cannot be applied to given types;
          UserDetails userDetails = readerRepository.findOne(username);
                                                    ^
  required: Example<S>
  found: String
  reason: cannot infer type-variable(s) S
    (argument mismatch; String cannot be converted to Example<S>)
  where S,T are type-variables:
    S extends Reader declared in method <S>findOne(Example<S>)
    T extends Object declared in interface QueryByExampleExecutor
1 error

> Task :compileJava FAILED

FAILURE: Build failed with an exception.


This compilation error is also picked up prior to compile time in my IDE (IntelliJ).
The problem seems to be caused by the type of the username parameter, which is of type String, but expected to be Example<S> where the type S should be UserDetails (I presume).
The other find methods on the repo object are working ok, but they return an object of type Optional<Reader>. So, I am able to do the following to get the code working:

UserDetails userDetails = readerRepository.findById(username).get();


The compilation error is confusing, because the Reader class does implement the UserDetails interface. But I am not familiar enough with the query-by-example mechanism to really understand what is happening here.
Can anyone help? Thanks a lot.

Kind regards,

Martin
19913750 (3) [Avatar] Offline
#2
I ran into the same issue.

I'm not positive I'm right, but this issue may be related to changes to methods in the JpaRepository interface hierarchy. The comments in the following StackOverflow post seem relevant: https://stackoverflow.com/questions/51027002/the-method-findoneexamples-in-the-type-querybyexampleexecutorcontact-is-no

Specifically

Some CRUD Repository methods got renamed in Spring Data and

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    T findOne(ID id);


is one of them. Now you should use the

public interface CrudRepository<T, ID> extends Repository<T, ID> {
    Optional<T> findById(ID id);



I'm not sure I'm interpreting correctly, in part because the only findOne() method I see is not (no longer?) in CrudRepository but is in QueryByExampleExecutor. Regardless, the following edits to the book's code at least eliminated the compiler error for me

//UserDetails userDetails = readerRepository.findOne(username);
//if (userDetails != null) {
//return userDetails;
//}

Optional<Reader> userDetails = readerRepository.findById(username);
if(userDetails.isPresent()) {
   return userDetails.get();
}