类型参数“S"的推断类型“S"不在其范围内;应该扩展 'ua.com.store.entity.Country [英] Inferred type 'S' for type parameter 'S' is not within its bound; should extend 'ua.com.store.entity.Country

查看:25
本文介绍了类型参数“S"的推断类型“S"不在其范围内;应该扩展 'ua.com.store.entity.Country的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

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

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

谢谢.

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

推荐答案

Spring 文档定义 getOne 方法如下

Spring documentation defines methods getOne as follows

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

在您的方法中,您的输入参数是 int 类型的id",但不限于接口示例.

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

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

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

Optional<T> findById(ID id)

根据你的实现你可以写它

According to your implementation you may write it

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

这篇关于类型参数“S"的推断类型“S"不在其范围内;应该扩展 'ua.com.store.entity.Country的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆