Java Reference
In-Depth Information
public void setMessageText(String messageText) {
this.messageText = messageText;
}
private String message;
}
The sole condescension to Hibernate here is the provision of a private default construc-
tor. Hibernate demands that all POJOs to be stored should provide a default constructor; but
even that can be worked around when third-party classes fail to satisfy this limited require-
ment (we will demonstrate this in Appendix A).
Origins of Hibernate and Object-Relational
Mapping
If Hibernate is the solution, what was the problem? One answer is that doing things the right
way when using JDBC requires a considerable body of code, and careful observation of various
rules (such as those governing connection management) to ensure that your application does
not leak resources. The gargantuan body of code in Listing 1-3 is required to populate the exam-
ple Motd object from the database even when you know the appropriate message identifier.
Listing 1-3. The JDBC Approach to Retrieving the POJO
public static List getMessages(int messageId) throws MessageException {
Connection c = null;
PreparedStatement p = null;
List list = new ArrayList();
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection(
"jdbc:hsqldb:testdb;shutdown=true",
"hibernate",
"hibernate");
p = c.prepareStatement(
"select message from motd");
ResultSet rs = p.executeQuery();
while(rs.next()) {
String text = rs.getString(1);
list.add(new Message(text));
}
return list;
Search WWH ::




Custom Search