Search code examples
javajodd

Is it possible bean mapping to resultset as apache dbutils in jodd?


I'm using the jodd framework and it provides lots of utility with dboom.

My question is, can I do simple pojo mapping to a result set like ResultSetHandler by apache dbutils with the jodd dboom module?

sql query :

select child.id as id, child.name as name, parent.name as parentCategoryName
from tblmcategory child left outer join tblmcategory parent
on parent.categoryId = child.parentCategoryId;` 

and pojo is like

public class CategoryData implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private String parentCategoryName;

    //setter and getters method....
}

can I map resultset to this pojo list..?

dao code ::

public <T> List<T> executeQuery1(String query, Class<T> clazz) throws DatabaseException {
        try {
            return jodd.db.oom.DbOomQuery.query(query).autoClose().list(clazz);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DatabaseException(e.getMessage());
        }
    }

From this I am getting correct count in list but it is list of null object....


Solution

  • Please see Jodd DbOom Naming Strategies. In DbOoom it is possible to register or configure your own naming strategy, that define how column names are converted to bean names. In your case this is what happens: since you have null objects in the list, this means that mapping failed.

    Default mapping rule/convention is the following: column name FOO_BAR is translated to fooBar. You are using different naming strategy, where column name equals to bean name.

    So you can do the following:

    • change the strategy to match your use case, or
    • rename database columns to common 'uppercase-with-underscore-separated-names' :)