Presents your JAVA E-NEWSLETTER for February 27, 2003 <-------------------------------------------> USE AN OBJECT RELATIONAL MAPPING API TO WRITE JDBC CODE Writing database and JDBC code can be tedious and prone to error. Object Relational Mapping (ORM) tools or APIs can alleviate some of these problems. Until recently, most of the available ORM tools were commercial and very expensive. In the past year, however, a handful of open source ORM tools and API's have become available. Most of the popular ORM tools work by using a file or files to map Java objects and relationships to a backend database. Depending on the tool, you may have to create the files yourself; or the ORM tool may access your database and automatically generate the files it needs to run against your database schema. After the mapping files are created, you can use them to generate source code, or you can use the ORM tool's API directly and allow it to generate the code it needs at runtime. After the mappings are set up and you've decided how you'll interact with the ORM tool, you can begin to replace your SQL code. Here's a method that uses an ORM API to fetch a list of review objects owned by a particular reviewer ID from a database: public Collection getAllForReviewerId(int reviewerId) throws DaoException { Criteria criteria = new Criteria(); criteria.addEqualTo("owner_id", new Integer(reviewerId)); QueryByCriteria query = new QueryByCriteria(Review.class, criteria); query.setRequestedAttribute("owner_id"); try { return broker.getCollectionByQuery(query); } catch (Throwable t) { t.printStackTrace(); } return null; } All good ORM tools support the database functionality you'll require, such as transactions, primary keys, foreign keys, and n-to-n relationships. Examples of ORM tools and API's include the Apache DB Project's ObJectRelationalBridge (OJB) and the open-source project Hibernate. Apache DB Project's ObJectRelationalBridge (OJB) http://db.apache.org/ojb/ Hibernate http://cl.com.com/Click?q=26-KqRVIiMfeeZEsSnnZHJRZLebQ767 Writing JDBC code can be a headache, but ORM APIs can lessen your workload without incurring great expense, thanks to open source development. ----------------------------------------