19

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);
    }
}
ns16
  • 1,322
  • 2
  • 17
  • 26
Roman Sychok
  • 319
  • 1
  • 2
  • 7

8 Answers8

16

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);
}
Lakshi
  • 490
  • 4
  • 10
4

A 100% working solution is following:

@Override
public Country findOne(int id) {
    return dao.findById(id).orElse(null);
}
Prabhu
  • 5,296
  • 4
  • 37
  • 45
3

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.

yang
  • 41
  • 3
  • Do you have some evidence to confirm this? – JJJ Mar 19 '19 at 08:16
  • It is exists in the jar lib whose name is spring-data-commons. spring-data-commons jar lib is dependent to spring-boot-starter-data-jpa lib. When my dependencies is `org.springframework.boot:spring-boot-starter-data-jpa:1.5.9.RELEASE`, the code `class org.springframework.data.repository.CrudRepository` `public interface CrudRepository extends Repository` – yang Mar 19 '19 at 14:52
  • When my dependencies is `org.springframework.boot:spring-boot-starter-data-jpa:2.0.1.RELEASE`, the code `org.springframework.data.repository.query.QueryByExampleExecutor` `public interface QueryByExampleExecutor` And when 2.0.1.RELEASE version, there is no CrudRepository interface file. by the way, there is one sample at https://github.com/spring-guides/tut-rest/issues/53 . – yang Mar 19 '19 at 14:52
  • Thanks for the replies, please edit them into your answer so they can help future visitors. :) – JJJ Mar 19 '19 at 14:52
3

I just solved this problem in my program. I don't use the findOne method, I was just saving initial data with the save method. It turns out that I had copied my repo interface from another repo interface, and forgot to update the object in "extends JpaRepository<Object, Long>" to the new object.

1

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);
}
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
1

This Worked for me...


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

Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
0

I got the same problem.

ua.com.store.entity.Country -> It's like adding an external entity

You must import the correct directory of the entity "Counrty" that you will use in your project. e.g

import com.RomanSyhock.Entity.Country;
0

Additionally inherit another interface for Repository, namely JpaSpecificationExecutor

public interface CountryDAO extends JpaRepository<Country, Integer>, JpaSpecificationExecutor<Country> {

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