Search code examples
springspring-jdbc

Unable to get table alias working with spring jdbcTemplate


I am trying select statement on a table with alias name. When I retrieve the resultset the alias doesn't seem to work with it.item_id. It works with item_id. Any idea where I am going wrong?

getJdbcTemplate().query((connection) -> {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM some_item_table AS it WHERE it.item_id = ?");
            preparedStatement.setString(1, 123);
            return preparedStatement;
        }, (rs, i) -> product()
                .setId(rs.getInt("it.item_id"))// NOT WORKING
                //.setId(rs.getInt("item_id")) THIS WORKS!
                ...
                );

Solution

  • In the result set the aliases are not available so it does not work.

    You could change your SQL query like SELECT it.item_id AS some_item_id * FROM some_item_table AS it then you can do rs.getInt("some_item_id").

    In your query you do not really need an alias as you have only one table.