Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
}
like image 318
Roman Sychok Avatar asked Oct 03 '18 19:10

Roman Sychok


3 Answers

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);
}
like image 182
Lakshi Avatar answered Oct 22 '22 12:10

Lakshi


A 100% working solution is following:

@Override
public Country findOne(int id) {
    return dao..findById(id).orElse(null);
}
like image 43
Ihsan Ullah Khan Avatar answered Oct 22 '22 12:10

Ihsan Ullah Khan


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);
}
like image 45
Shahid Hussain Abbasi Avatar answered Oct 22 '22 12:10

Shahid Hussain Abbasi