I am along the way of making a front-end for any Database Driven application and may use top tips. I've the next fundamental organizations during my database:

  • aspect
  • aspect_value

As possible image I'm able to have numerous aspect value to every aspect to ensure that whenever a user records an element they are able to choose several value per aspect... simple.

I have produced an POJO entity to model each aspect an my real question is this... using Spring and also the jdbcTemplate wouldso would I have the ability to produce the preferred composite relationship using org.springframework.jdbc.core.RowMapper i.e. each aspect object that contains a number of aspect value ojects? And for your matter I'd appreciate should you could please tell me if this is the easiest method to get it done. I am keen sooner or later to delve much deeper into ORM but I have been delay to date by the amount of issues I have experienced that has slowed down lower my development and brought to the choice to use jdbcTemplate rather.

Thanks

Use a RowMapper if you're storing aspect_values inside your aspect object as objects. Each call to RowMapper returns an item so you'll finish track of an accumulation of aspect_values. If you want to build an element object (or objects) with values included in the aspect_value table a ResultSetExtractor is the foremost choice.

Listed here are my good examples as guaranteed. I must type these in manually because our development network is with an internal network only so any typos are copy errors and never errors within the code. They are abbreviated versions of inner classes during my DAO:

This maps just one row within the ResultSet for an object:

public List<MessageSummary> getMessages(Object[] params)
{
  // mList is filled with objects created in MessageRowMapper,
  // so the length of the list equal to the number of rows in the ResultSet
  List<MessageSummary> mList = jdbcTemplate.query(sqlStr, new MessageRowMapper(),
                                                  params);
  return mList;
}

private final class MessageRowMapper implements RowMapper<MessageSummary>
{
  @Override
  public MessageSummary mapRow(ResultSet rs, int i) throws SQLException
  {
    MessageSummary ms = new MessageSummary();

    ms.setId(rs.getInt("id"));
    ms.setMessage(rs.getString("message"));

    return ms;
  }
}

ResultSetExtractor creates exactly the same idea except you map the whole set yourself rather than just transforming a row into an item. This really is helpful whenever your object has characteristics from multiple rows.

public Map<Integer, List<String>> getResults(Object[] params)
{
  Map<Integer, List<String>> result = jdbcTemplate.query(sqlStr, new ResultExtractor(),
                                                         params);
  return result;
}

private final class ResultExtractor implements ResultSetExtractor<Map<Integer, List<String>>>
{
  @Override
  public Map<Integer, List<String>> extractData(ResultSet rs)
                                    throws SQLException, DataAccessException
  {
    Map<Integer, List<String>> resultMap = new HashMap<Integer, List<String>>();

    while (rs.next())
    {
      int id = rs.getInt("id");
      List<String> nameList = resultMap.get(id);
      if (nameList == null)
      {
        nameList = new ArrayList<String>();
        resultMap.put(id, nameList);
      }
      nameList.add(rs.getString("name"));
    }
    return resultMap;
  }

}

The RowMapper interface supplies a method

Object mapRow(ResultSet rs,
          int rowNum)
          throws SQLException

You implement this process inside a class and supply code to populate your entity object with values locked in the row from the ResultSet rs. To get the resultset itself from database , you should use JdbcTemplate.choose method's overload

List jdbcTemplate.query(String sql, RowMapper mapper )