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.
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);
}
}
Spring documentation defines methods getOne as follows
<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.
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);
}
A 100% working solution is following:
@Override
public Country findOne(int id) {
return dao..findById(id).orElse(null);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With