Friday, February 8, 2008

Named Query Setup

this works for both hql and native query
/**
* Return a list of objects using a named query.
*
* @param queryName name of the query to be run
* @param parameterMap map of parameter names and values
* @return a List of all objects returned by the query
*/
protected List executeNamedQuery(String queryName,
Map parameterMap) {
try {
queryName = getPersistentClass().getName() + "." + queryName;
Query query = currentSession().getNamedQuery(queryName);
prepareQuery(query);
for (Map.Entry entry : parameterMap.entrySet()) {
applyNamedParameterToQuery(query, entry.getKey(), entry.getValue());
}
List list = query.list();
return list;
} catch (DataAccessException e) {
throw e;
}
}


/**
* Apply the given name parameter to the given Query object.
*
* @param query the Query object
* @param paramName the name of the parameter
* @param value the value of the parameter
* @throws org.hibernate.HibernateException
* if thrown by the Query object
*/
protected void applyNamedParameterToQuery(Query query, String paramName, Object value)
throws HibernateException {

if (value instanceof Collection) {
query.setParameterList(paramName, (Collection) value);
} else if (value instanceof Object[]) {
query.setParameterList(paramName, (Object[]) value);
} else {
query.setParameter(paramName, value);
}
}

protected void prepareQuery(Query query) {
SessionFactoryUtils.applyTransactionTimeout(query,
sessionFactory);
}

MAPPING EXAMPLE







Tuesday, February 27, 2007

Hibernate Criteria

DetachedCriteria criteria = DetachedCriteria.forClass(MyObject.class);
criteria.add(Restrictions.ge("prop1Name", valueObject);
criteria.add(Restrictions.eq("prop2Name", valueObject);
//getHibernateTemplate() returns org.springframework.orm.hibernate3.HibernateTemplate
List list = getHibernateTemplate().findByCriteria(criteria);

Monday, February 26, 2007

Spring Basics

GET BEAN OUT OF SPRING CONFIG FILE

//define location of the config files referenced by the bean(s)
ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(
new String[]{"classpath*:config1.xml",
"classpath*:/subdir/config2.xml",
"classpath*:etc.xml"});
//get the bean
MyBean myBean = (MyBean)springContext.getBean("myBeanName");

Friday, February 23, 2007

Buzz Stuff

Saas is a new, or maybe not so new buzz word. It stands for Software as a service. It's a web 2.0 rendition of ASP. Of course, there are new characteristics and subtleties attached, but concepts are pretty similar.
As always, wikipedia has the best definition

On-demand Software is a software that runs across the internet in a software as a service model (no software to install).

Service Oriented Architecture (SOA) aims to simplify integration and increase agility by creating well defined and standards-based boundaries between systems.

Apache Collection Utilities

CollectionUtils, SetUtils, Predicates, etc.
o'reilly reference

Friday, January 26, 2007

Hibernate - map column to a non-existent java property

On rare occasions, I had to populate a database column that doesn't have a matching property in the object model. Say, I have a not null column INTERFACE_ID and value for this column is derived based on some logic. Here is how I worked around it in Hibernate.

1. Create implementation of a UserType that has logic to derive a value for interface_id
2. Use the following mapping

<property name="interfaceId" column="INTERFACE_ID" access="noop" update="false" null="false" type="SomeUserTypeImplementation"/>

3. A few things to note...
⇒ name attribute is any madeup value
⇒ acces="noop" tells hibernate not to look for an object property for this column
⇒ not-null="false". noop columns must be defined as nullable
⇒ update="false". noop columns cannot be updated