Java 类型参数“S”的推断类型“S”不在其范围内;应该扩展 'ua.com.store.entity.Country

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52634362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:32:55  来源:igfitidea点击:

Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'ua.com.store.entity.Country

javaspringspring-mvcspring-boot

提问by Roman Sychok

I have a problem with my CountryServiceImpl,when I want realize method findOne in CountryServiceImpl it tells me "Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'ua.com.store.entity.Country".

我的 CountryServiceImpl 有问题,当我想在 CountryServiceImpl 中实现 findOne 方法时,它告诉我“类型参数 'S' 的推断类型 'S' 不在其范围内;应该扩展 'ua.com.store.entity.Country” .

I wanted to fix by myself, but I don't understand what this means. Could you please help me with this issue.

我想自己修复,但我不明白这是什么意思。你能帮我解决这个问题吗?

Thank you.

谢谢你。

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String countryName;

    @OneToMany(mappedBy = "country")
    private Set<Brand> brands = new HashSet<Brand>();
}


public interface CountryDAO extends JpaRepository<Country, Integer> {

    @Query("from Country c where c.countryName=:name")
    Country findByCountryName(@Param("name") String name);
}


public interface CountryService {

    void save(Country country);
    void delete(Country country);
    List<Country> findAll();
    Country findOne(int id);
    Country findByCountryName(String name);
}


@Service
public class CountryServiceImpl implements CountryService {

    @Autowired
    private CountryDAO dao;

    @Override
    public void save(Country country) {
        dao.save(country);
    }

    @Override
    public void delete(Country country) {
        dao.delete(country);
    }

    @Override
    public List<Country> findAll() {
        return dao.findAll();
    }

    @Override
    public Country findOne(int id) {
        return dao.findOne(id);
    }

    @Override
    public Country findByCountryName(String name) {
        return dao.findByCountryName(name);
    }
}

采纳答案by Lakshi

Spring documentation defines methods getOne as follows

Spring 文档定义了 getOne 方法如下

<S extends T> Optional<S> findOne(Example<S> example)

In your method your input parameter is 'id' of type int but not bounded to interface Example.

在您的方法中,您的输入参数是 int 类型的“id”,但不受接口示例的限制。

To find an entity with it 'id' you can use the method

要查找带有“id”的实体,您可以使用该方法

Optional<T> findById(ID id)

According to your implementation you may write it

根据您的实现,您可以编写它

@Override
public Country findOne(int id) {
    return dao.findById(id);
}

回答by yang

It is possible to be relevant about spring-boot version. I meet the same issue when my spring-boot version is 2.0.1.RELEASE. But after change the spring-boot version to the 1.5.9.RELEASE, it is resolved.

可能与 spring-boot 版本相关。当我的 spring-boot 版本是 2.0.1.RELEASE 时,我遇到了同样的问题。但是把spring-boot版本改成1.5.9.RELEASE后就解决了。

回答by Ihsan Ullah Khan

A 100% working solution is following:

100% 工作解决方案如下:

@Override
public Country findOne(int id) {
    return dao..findById(id).orElse(null);
}

回答by Shahid Hussain Abbasi

You need to change from

你需要从

public T getOne(ID id) {
        return repository.getOne(id);
}

To

public Optional<T> getOne(ID id) {
        return repository.findById(id);
}