javaplayframeworkebean

SQLException:Column Index out of range with Ebean and rawSql in Play Framework


After looking for a day I still don't get what I am doing wrong here. I want to select the column name in my table Laboratory in the server which contain 6 columns but I keep getting the error:

"SQLException:Column Index out of range: 0, number of columns: 1".

Is anyone familiar with this? Thanks

Here's my code:

    String sql = "SELECT name FROM Laboratory";
    RawSql rawSql = RawSqlBuilder
            .parse(sql)
            .create();
    Query<Laboratory> query = Ebean.find(Laboratory.class);
    query.setRawSql(rawSql).where();
    List<Laboratory> results = query.findList();

    results.sort(Comparator.comparing(Laboratory::getName)); // Sort by alphabetical order
    try {
        String json = JsonHelper.toJSON(results);
        return ok(json).as("application/json");
    } catch (JsonProcessingException e) {
        LOG.error("Unable to serialize sql request", e);
        return internalServerError();
    }

Solution

  • Ok I have found the solution. It turns out that I just had to fetch the id in addition to the name or else Ebean was lost. In the end here is the sql request that works:

    String sql = "SELECT id, name FROM Laboratory";