Fetching selected Data

44 views Asked by At

I am new to spring I want to get all the data whose id=given by me. Can any one tell me how can I do That

@Override
    public Collection<Device> listDevice() {
        Collection<Device> deviceCollection=new ArrayList<>();
        Iterable<Device> deviceIterable=deviceRepository.findAll();
        for(Device d:deviceIterable){
            deviceCollection.add(d);
        }
        return deviceCollection;
}

This is giving me all the data But I want data having id

long id=device.getManufacturer_id();

please tell how to do that

2

There are 2 answers

1
Andres On

Instead of Repository#findAll, use Repository#findById(ID primaryKey).

Check this documentation:

http://docs.spring.io/spring-data/jpa/docs/1.0.0.M1/reference/html/

0
vikeng21 On

Instead try SpringDao library for JDBC related operations in Spring framework. It gives lots of pre built classes for requirements like your like NamedParameterJdbcDaoSupport, JdbcDaoSupport etc.

Will find host of examples of that on the net. just google it.