John Lenon
John Lenon

Reputation: 143

spring boot, jdbcTemplate, Java

I have a query that takes one column row from database and I want to set this data to my model. My model name is Pictures and my method is follow:

@Override
public Pictures getPictureList() throws Exception {
    JdbcTemplate jdbc = new   JdbcTemplate(datasource);
    String sql= "select path from  bakery.pictures where id=1";
    Pictures pcList = jdbc.query(sql, new BeanPropertyRowMapper<Pictures>(Pictures.class));
    return pcList;
}

This method returns "a query was Inferred to list". How can I solve it?

Upvotes: 1

Views: 51

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44942

Use JdbcTemplate.queryForObject() method to retrieve a single row by it's primary key.

Pictures p = jdbc.queryForObject(sql, 
        new BeanPropertyRowMapper<Pictures>(Pictures.class));

JdbcTemplate.query() will return multiple rows which makes little sense if you are querying by primary key.

List<Pictures> list = jdbc.query(sql, 
        new BeanPropertyRowMapper<Pictures>(Pictures.class));
Picture p = list.get(0);

Upvotes: 1

Related Questions