Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Querying for persistent objects without a query language

A powerful combination of Hibernate and JavaServer Faces

  • Print
  • Feedback

Page 2 of 2

Figure 2. Input to database and back again without query language. Click on thumbnail to view full-sized image.

The mapping of the HTML select box options and the object attribute is completed in the faces-config.xml file. This XML file is the central configuration file for Webpages based on JavaServer Faces (JSF):

Listing 5. Query by example applied

 <faces-config>
   <converter>
<description>Converts "" to null</description>
      <converter-id>emptyStringToNull</converter-id>
<converter-class>com.bachlmayr.hibcrit.jsf.
EmptyStringToNullConverter</converter-class>
   </converter>
   <!-- Managed Beans -->
   <managed-bean>
      <description>Address</description>
      <managed-bean-name>Address</managed-bean-name>
<managed-bean-class>com.bachlmayr.hibcrit.
pojo.Address</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
   ...
</faces-config>



The JSF mapping enables the reference of business objects in the HTML code. The reference is called by the managed-bean-name and the attributes of the business object, visualized in Listing 7. When empty HTML input fields are posted, according to the HTTP standard, they appear as empty strings ("") in the business logic. But when leaving an attribute empty in a query, we usually want to ignore this attribute, instead of searching for a name such as "". JSF offers a variety of converters (see the JavaServer Faces specification) to translate an HTML input to a Java type. Custom converters can be created by implementing the Converter interface. In this article's example, empty strings are converted to null. Therefore, those empty arguments are ignored when building an example query. The custom converter implementation can be seen in the code example below:

Listing 6. JSF custom converter class

 public class EmptyStringToNullConverter implements Converter{
   
   /** Convert from presentation view to model view */
   public Object getAsObject(FacesContext ctx, 
      UIComponent comp, String value) {
         return (!value.equals(""))? new String(value) : null;
      }
   
   /** Convert other way round */
   public String getAsString(FacesContext ctx, 
      UIComponent comp, Object value) {
      ... ... // More code here
   }
}



The custom converter is referenced within the input text field tag to replace a potential empty string posted by the HTML form. The attribute name of the KeyAccountMgr object is treated as a null value in the business logic if the field remains empty:

Listing 7. Converted JSF used in HTML code

 <!— corresponding HTML code -->
<h:inputText value="#{KeyAccountMgr.name}" id="name" converter="emptyStringToNull"/>
<!— end of HTML snippet -->



Conclusion

Most Web applications need to persist data from data-entry forms or the like. The main issues in existing persistence strategies include the flexibility of queries, initial development effort, and maintainability. Hibernate addresses these challenges through the use of programmatic queries. The possibility of using reflection and having the compiler check the code improves time to market and flexibility, and the centralized mapping helps minimize maintenance effort. Accordingly, Hibernate can offer real advantages compared to using plain JDBC or Enterprise JavaBeans for achieving persistence. On the downside, while Hibernate is mostly compatible with the upcoming EJB 3 standard, it still does not implement it. Therefore, switching to another persistence runtime causes more effort when compared to the effort of changing runtimes that implement the same standard, provided the specification does not allow discrepancies of deployment descriptors or configuration files.

About the author

As a software consultant focusing on J2EE architecture, Gerald Bachlmayr is presently working on a development project in the finance industry, which will be rolled out for a number of banks in Germany shortly. In his eight years in software design and development, he has served in several positions, including development team lead, software architect, and quality assurance team lead. From a technical perspective, he focuses on J2EE technologies such as Hibernate, EJB, Struts, application servers, database modeling, and XML. He is a Sun-certified developer for business and Web components and for the Java 2 Platform. In the past, he has worked for industries such as banking, online brokerages, aircraft construction, aviation, telecommunication, and publishing.

Read more about Tools & Methods in JavaWorld's Tools & Methods section.

  • Print
  • Feedback

Resources