When mapping a row from a ResultSet, how should I handle foreign keys that represent domain objects?
Example: When loading a row from the Order table, there is a FK to the Customer table. I would like the Order object that is returned by OrderRowMapper.mapRow() to have a fully populated Customer object rather than just an integer FK.
I see two ways to do this:
1. give the RowMapper a reference to CustomerDAO so it can populate the Customer object immediately when FK is retrieved.
2. rewrite the SQL query so it JOINs the customer table and returns customer data as well. Instantiate a new Customer object within OrderRowMapper.mapRow() and call all the setters with customer data from the ResultSet.
Neither of these options seems very clean. (1) is bad from a performance standpoint; now there are two separate DB calls to populate the data. Or even more, since the CustomerRowMapper might be doing the same thing with some of it's FKs. (2) is faster but now the logic for loading Customer objects is no longer centralized in the CustomerDAO.
Does anyone have a better solution or opinion about these ideas? Of course, Hibernate is the best answer but political constraints force me to go with JDBC.
Thank you!
Roman


Reply With Quote