1

I have two services defined:

Class cars
 model, year, mileage

Class driver
 name, age, licExpDate

I've put queries for each in their own service i.e. queries relating to cars in car and queries relating to drivers in driver. Furthermore I have RowMapper in each service

class CarRowMapper implements RowMapper<Car>{
    @Override
    public Car mapRow(ResultSet rs, int rowNum) throws SQLException {
        Car car = new Car();
                car.setModel(rs.getString("model"));
        return car;
    }       
}

Question Now I need to export some data out for which I need to write a query which will involve columns for both car and driver in one query:

For example:

SELECT A.CAR B.NAME FROM CAR A, DRIVER B WHERE A.ID = B.ID

So, now how can I have multiple classes in my RowMapper so that I can write a method like this:

public List<Cars> carsWithDrivers() {
  String query "...";
  return service.getJdbcTemplate().query(query, Row_mapper_here?));
}
birdy
  • 9,286
  • 24
  • 107
  • 171
  • i think you need to have a separate domain object CarWithDriver and a CarWithDriverRowmapper – Vinay Nov 01 '12 at 10:47
  • Or use a [tuple](http://stackoverflow.com/questions/2670982/using-tuples-in-java) to hold Car and Driver. – pd40 Nov 16 '12 at 02:08

1 Answers1

0

Lets say the class you are going to use implementing RowMapper is MapCarWithDriver. Now inside it, there will be a mapRow(ResultSet, int rowNum) method being overridden. This will be called for each row of the resultSet of executed Query. So now you can create you own Driver object by

Driver driver1 = new Driver();
driver1.setName(rset.getString("NAME");
Car car1= new Car(driver1);
car1.setModel(rset.getString("CAR");

Rup Majumder
  • 241
  • 3
  • 6
  • 13