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

8

25

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);
    }
}
Capitulate answered 3/10, 2018 at 19:27 Comment(0)
L
18

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);
}
Lactometer answered 3/10, 2018 at 19:51 Comment(0)
R
5

A 100% working solution is following:

@Override
public Country findOne(int id) {
    return dao.findById(id).orElse(null);
}
Repeater answered 21/8, 2019 at 16:20 Comment(0)
P
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.

Pingpingpong answered 19/3, 2019 at 7:57 Comment(4)
Do you have some evidence to confirm this?Wastrel
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<T, ID extends Serializable> extends Repository<T, ID>Pingpingpong
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<T> And when 2.0.1.RELEASE version, there is no CrudRepository interface file. by the way, there is one sample at github.com/spring-guides/tut-rest/issues/53 .Pingpingpong
Thanks for the replies, please edit them into your answer so they can help future visitors. :)Wastrel
O
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.

Ojeda answered 17/7, 2020 at 21:23 Comment(1)
thanks. without your answer, I may not think about this.Asinine
E
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);
}
Earthbound answered 22/11, 2019 at 6:25 Comment(1)
getOne is method of JpaRepository and findById is method of CrudRepository, I think you will understand nowEarthbound
G
1

This Worked for me...


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

Graphite answered 4/8, 2020 at 8:38 Comment(0)
S
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;
Sniffy answered 4/11, 2021 at 14:46 Comment(0)
O
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);
}
Oliveolivegreen answered 27/5, 2023 at 2:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.