我想编写作用于例如从 Spring 扩展的类的函数JpaRepository<TestEntity, Integer>
。
我试过:实体:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class TestEntity {
@Id
Integer a;
Integer b;
}
回购:
import org.springframework.data.jpa.repository.JpaRepository;
public interface TestEntityRepository extends JpaRepository<TestEntity,Integer> {}
服务类看起来像这样:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import javax.persistence.Entity;
import java.util.function.Function;
@Service
public class TestEntityService {
@Autowired
TestEntityRepository testEntityRepository;
private Function<? extends JpaRepository<? extends Entity, Integer>, Boolean> doSomeThing = integerJpaRepository ->
integerJpaRepository.findAll().isEmpty();
public Boolean dbIsEmpty(){
return doSomeThing.apply(testEntityRepository);
}
}
但是,我在 Intellij 中遇到错误:
函数中的 apply (capture < ? extends org.springframework.data.jpa.repository.JpaRepository< ? extends javax.persistence.Entity,java.lang.Integer>>) 不能应用于 (.java_tests.database.TestEntityRepository)