简体   繁体   中英

Ambiguous method call in Intellij with Spring Data JPA interface

I have an odd ambiguous method call error being reported by intellij. I am using a spring data JPA interface that also implements an interface with the same method signature:

UserRepository:

@Repository
public interface UserRepository extends CrudRepository<User, String>, UserProvider {
} 

UserProvider:

public interface UserProvider {
    User findOne(String userId);
}

Reasons for needing the second interface aside, I assumed that since these are interfaces and the method signatures for findOne are the same, the compiler would have no problem dealing with the ambiguity. Am I not understanding Java right, or is this an intellij problem? Is it an issue with the CrudRepository interface resolving through generics?

I've faced the same issue. In my case there was no error reported by my IDE (Eclipse), but when building the project via Maven from console I've got the error about method abiguity during the testCompile.

I was suspecting this is realted to different Java compiler being used by Eclipse and Maven, but the problem existed even when I verified they both use the same JDK (jdk1.8.0_91).

However, after few hours spent on this, I managed to overcome this by annotating my repository with @RepositoryDefinition instead of extending it from CrudRepository. Using your sample, it would be:

@RepositoryDefinition(domainClass = User.class, idClass = String.class)
public interface UserRepository extends UserProvider {
}

I believe findOne is now findById , which I ran into the same error for. I found that even though IntelliJ reports the methods as having the same signature, they don't actually have the same signature because CrudRepository 's was generic whereas mine was not.

I believe if you updated UserProvider to be generic, the error would go away:

// UserRepository
@Repository
public interface UserRepository extends CrudRepository<User, String>, UserProvider {
} 
// UserProvider
public interface UserProvider<E, ID> {
    E findOne(ID userId);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM