I've spend some time recently putting together the DAOs for my project. Quite some time, in fact. God, they are ugly.
The Transfer Objects, the ones that ones that you end up using outside the persistence layer, seem usually to be coded as beans. So, the class which builds these objects has to set a bunch of properties:
There are similar (but not identical) bits of code all through this class; binding parameters into various bits of SQL, that sort of thing:
final String sql =
+ StringUtils.join(MSSqlPrincipalDAO.FIELDS, ", ")
+ " "
+ "from "
+ " "
+ "where "
+ StringUtils.join(MSSqlPrincipalDAO.KEYS, " = ? " + "and ")
+ " = ? ";
final PreparedStatement statement = connection.prepareStatement(sql);
To my Python-educated eye, this looks hideous. The field names are repeated all over the place, and putting them into constants isn't any better. The bean-property to SQL column mapping is also repeated several times - very error prone. But in Java, I can't think of a cleaner, less repetitive way of doing it. Am I missing something super-elegant?
Another question; I went through this code replacing all the SQL construction literals (like
"select ", for example) with constants. But it seemed to me that this made the code less readable, and no more maintainable. So I'm leaving the literals in. Will the code-police be able to make a good case against me?
BTW, clearly these DAOs could be generated automatically. Are there any good tools for this? (Other than sql2java, which I've tried; I can't get it to work.) If not, I think I feel some Python coming on...Posted to Java by Simon Brunning at February 19, 2004 02:06 PM