English 中文(简体)
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);
    }
}
最佳回答

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

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.

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.

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

This Worked for me...


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

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;

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签