Wednesday, 13 November 2013

Enhancing Apache DBUtils when column names do not match getter & setter names

In a recent post on running stored procedures I made use of Apache's DBUtils package as a basic ORM.

I had a problem where certain fields in my POJO were not getting populated.
The reason being that DBUtils does not make use of the '@Column' annotation and therefore some extra code is needed.

In our case it occurred for every column that had an underscore in the name but not in the POJO getters & setters.

A snipped from my POJO could look like this.

@Column(name="WIND_DIRECTION", nullable=true)
public String getWindDirection()
{
return mWindDirection;
}
public void setWindDirection(String aWindDirection)
{
mWindDirection = aWindDirection;
}

Using the same Dao in the post I refferred to above the ResultSetHandler will now be created as follows:

final Map<String, String> columnToPropertyOverrides = new HashMap<>();
columnToPropertyOverrides.put("WIND_DIRECTION", "WindDirection");
BeanProcessor dateForProcessor = new BeanProcessor(columnToPropertyOverrides);
BasicRowProcessor rp = new BasicRowProcessor(dateForProcessor);
ResultSetHandler<List<T>> resultSetHandler = new BeanListHandler<T>(aType, rp);
view raw enhanced Dao hosted with ❤ by GitHub

No comments: