Archive for September, 2006

Implement “Search results” using Hibernate

September 29, 2006

Hibernate Criteria API: Multi-Criteria Search Made Easy

Powerful, Elegant and Definitely Worth a Try,

that’s what the article says & it’s mighty true. Have seen quiet some people constructing these kind of queries using StringBuffer & stuff, but this is waaaay more elegant :)

..
Criteria criteria
= session.createCriteria(Accommodation.class);
if (startDate != null) {
criteria.add(Expression.ge("availabilityDate",
startDate);
}
if (endDate != null) {
criteria.add(Expression.le("availabilityDate",
endDate);
}

This beats

if (startDate != null) {
queryBuf.append(firstClause ? " where ": " and ");
queryBuf.append("a.availabiliyDate >=:startDate");
parameters.put("startDate",startDate);
firstClause = false;
}

any time !!

Must never do Antipatterns – Exception Handling

September 28, 2006

From java.net: Exception-Handling Antipatterns

Antipatterns
Log and Throw

Example:

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw e;
}

or

catch (NoSuchMethodException e) {
LOG.error(“Blah”, e);
throw new MyServiceException(“Blah”, e);
}

or

catch (NoSuchMethodException e) {
e.printStackTrace();
throw new MyServiceException(“Blah”, e);
}

All of the above examples are equally wrong. This is one of the most annoying error-handling antipatterns. Either log the exception, or throw it, but never do both. Logging and throwing results in multiple log messages for a single problem in the code, and makes life hell for the support engineer who is trying to dig through the logs.

I see this in so many “commercial” products that I want my money back !!; makes debugging a pain the neck. Seeing the same trace logged from every tier again & again is not pretty.

Hidden variables in DisplayTag

September 27, 2006

If you are using DisplayTag and need to have hidden variables like I do (I’m using Struts) then this is a nice way

…setting the ’s class and headerClass attributes to hidden, which is a CSS style whose display property is none. This is a simple but effective method for keeping data available in the request scope without having to display it to the user.

Code snip :

<display:column property=”id” title=”ID” class=”hidden” headerClass=”hidden” media=”html” />

Source : Creating Highly Functional Tables in JSP Using DisplayTag and JavaScript