Hello List.
I added some methods to GenericDao and of course to GenericDaoHibernate, may
be it is useful to anyone else...
The idea is get a list of objects [ordered if necesary] of a Type using it
parent as a filter... For example: Country -> State, I want to get all
states of US... then i tell the StateDao who is it parent (country), finally
I can use the stateDao to get that list.
This is for the interface GenericDao
/**
* Sets the mapped parent attribute
* @param propiedadPadre the father attribute
* @see GenericDao#listByParent(Serializable)
* @see GenericDao#listByParent(Serializable, String)
*/
void setParentAttribute(String propiedadPadre);
/**
* Lets add an order to obtain lists, as is a map used to represent
* the keys you want to add property and a value representing the
* order either ascending or descending order for the attribute.
* @param order map with keys (attributes) and values (asc or desc)e
* @see GenericDao#listarPorPadre(Serializable)
* @see GenericDao#listarPorPadre(Serializable, String)
*/
void setOrder(Map<String,String> order);
/**
* fetches all the objects <pre>T</pre> whose father is
<pre>objetoPadre</pre>
* @param objetoPadre the parent
* @return all the objects <pre>T</pre> whose father is
<pre>objetoPadre</pre>
*/
List<T> listByParent(Serializable objetoPadre);
/**
* fetches all the objects <pre>T</pre> whose father is
<pre>objetoPadre</pre>
* @param objetoPadre the parent
* @param propertyParent the maped parent attribute
* @return fetches all the objects <pre>T</pre> whose father is
<pre>objetoPadre</pre>
*/
List<T> listByParent(Serializable objetoPadre, String propiedadPadre);
This is the code to add in the implementation in HibernateGenericDao
private String propiedadPadre;
private Map<String, String> order;
/**
* @param propiedadPadre the propiedadPadre to set
*/
public void setParentAttribute(String propiedadPadre) {
this.propiedadPadre = propiedadPadre;
}
public void setOrder(Map<String,String> order) {
this.order = order;
}
/**
* {...@inheritdoc}
*/
public List<T> listByParent(Serializable objetoPadre) {
return listByParent(objetoPadre, propiedadPadre);
}
/**
* {...@inheritdoc}
*/
@SuppressWarnings("unchecked")
public List<T> listByParent(Serializable objetoPadre, String
propiedadPadre) {
logger.info("objetoPadre: " + objetoPadre + ", propiedadPadre: " +
propiedadPadre + ", persistentClass: " + persistentClass);
Assert.notNull(objetoPadre);
Assert.notNull(propiedadPadre);
Assert.notNull(persistentClass);
DetachedCriteria crit = DetachedCriteria.forClass(persistentClass);
crit.add(Expression.eq(propiedadPadre, objetoPadre));
if(order != null){
Set<String> keys = order.keySet();
for (String property : keys) {
Integer index = property.indexOf(".");
if (index != -1){
String alias = property.substring(0, index);
log.debug("Creando alias: " + alias);
crit.createAlias(alias, alias);
}
String asc = order.get(property);
//by default, the ordering is ascending, even it is empty.
log.debug("property: " + property + ", ordering: " + asc);
if("desc".equalsIgnoreCase(asc)){
crit.addOrder(Order.desc(property));
} else {
crit.addOrder(Order.asc(property));
}
}
}
return getHibernateTemplate().findByCriteria(crit);
}
And finally we can let spring inject the required data as follow...
<bean id="stateDao"
class="cl.etc.dao.hibernate.GenericDaoHibernate">
<constructor-arg value="cl.etc.model.State"/>
<property name="sessionFactory" ref="sessionFactory"/>
<property name="parentAttribute" value="country"/>
<property name="order">
<map>
<entry key="name">
<value>asc</value>
</entry>
</map>
</property>
</bean>
Hope this can help...
Best Regards
Oscar