I'd like to optimize some Hibernate code.
I try to tuning Hibernate and Spring, I wonder if it's right if I only have to add the hibernate cache property "true":
<prop key="hibernate.cache.use_query_cache">true</prop>
and I have the SessionFactory defined in Spring ("aop.xml") :
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.connection.datasource">java:comp/env/jdbc/hibernateCode</prop>
<prop key="hibernate.connection.pool_size">50</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
And finally I try to use the LazyDataModel, the method implemented:
@Override
public List<EnvioMensual> lazyLoad(
final int index,
final int count,
final String sortField,
final boolean sortOrder,
final Map<String, String> filters,
EnvioMensual filtro) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvioMensual.class);
if (sortField != null && !sortField.isEmpty()) {
if (sortOrder) {
criteria = criteria.addOrder(Order.asc(sortField));
} else {
criteria = criteria.addOrder(Order.desc(sortField));
}
}
if (!filters.isEmpty()) {
final Iterator<Entry<String, String>> iterator = filters.entrySet().iterator();
while (iterator.hasNext()) {
final Entry<String, String> entry = iterator.next();
criteria = criteria.add(
Restrictions.like(
entry.getKey(),
entry.getValue(),
MatchMode.START
)
);
}
}
criteria = criteria.setFirstResult(index).setMaxResults(count);
return criteria.list();
}
The queries get correctly parameters :
final Criteria criteria = sessionFactory.getCurrentSession()
.createCriteria(EnvioTraspaso.class);
if (criteriosFiltrado.getFechaCreacion() != null
&& !criteriosFiltrado.getFechaCreacion().equals("")) {
final Calendar criterioFecha = criteriosFiltrado.getFechaCreacion();
final Calendar fechaMinima = CalendarUtils.getFechaMinima(criterioFecha);
final Calendar fechaMaxima = CalendarUtils.getFechaMaxima(criterioFecha);
final Conjunction and = Restrictions.conjunction();
and.add(Restrictions.ge("fechaCreacion", fechaMinima));
and.add(Restrictions.lt("fechaCreacion", fechaMaxima));
criteria.add(and);
}
Thanks Amir!! I have tests that take a very long time to respond, then I need to fix my code this way:
@Override
@Transactional
public Set<NotificacionSISNOT> filterBySujeto(final Sujeto sujeto, final int start, final int limit, final String orderBy) {
final Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);
criteria.add(Restrictions....
...criteria.setFirstResult(start)
.setMaxResults(limit)
.setCacheable(true)
.setCacheRegion("sisnotpages");
return new LinkedHashSet<NotificacionSISNOT>(criteria.list());
}
It's correct ?