-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria Query Not Working
PostPosted: Mon Feb 12, 2007 3:13 pm 
Newbie

Joined: Thu Jan 11, 2007 5:53 pm
Posts: 3
Hi, I'm developing an application using Spring 2 + JPA + Hibernate, and I got some problem trying to create a 'find' method for a Generic DAO using the Criteria API. Here are the codes:

applicationContext.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
         </bean>
      </property>
      <property name="loadTimeWeaver">
         <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
      </property>
   </bean>

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://server01/database"/>
      <property name="username" value="root"/>
      <property name="password" value="pwd001"/>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
      <property name="dataSource" ref="dataSource"/>
   </bean>

   <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
      <property name="transactionManager" ref="transactionManager"/>
      <property name="transactionAttributes">
         <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
         </props>
      </property>
   </bean>
   
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.showsql">true</prop>
         </props>
      </property>
      <property name="dataSource" ref="dataSource"/>
   </bean>

   <bean id="estadoDAO" parent="baseTransactionProxy">
      <property name="target">
         <bean class="app.dao.GenericDAOImpl">
            <constructor-arg>
               <value>app.model.Estado</value>
            </constructor-arg>
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="sessionFactory" ref="sessionFactory"/>
         </bean>
      </property>
   </bean>

</beans>


GenericDAOImpl.java

Code:
public class GenericDAOImpl<T> extends JpaDaoSupport
      implements GenericDAO<T> {

   private Class<T> type;
   private SessionFactory sessionFactory;

   public GenericDAOImpl(Class<T> type) {
      this.type = type;
   }
   
   public SessionFactory getSessionFactory() {
      return this.sessionFactory;
   }
   
   public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
   }

   public void delete(T entity) {
      getJpaTemplate().remove(entity);
   }

   public T save(T entity) {
      getJpaTemplate().persist(entity);
      return entity;
   }

   public T update(T entity) {
      return getJpaTemplate().merge(entity);
   }

   public T findById(Long id) {
      return getJpaTemplate().find(type, id);
   }

   @SuppressWarnings("unchecked")
   public List<T> findAll() {
      return getJpaTemplate().find("FROM " + type.getName());
   }

   public List findByParam(List<Criterion> params) {
      SessionFactory sessionFactory = this.getSessionFactory();
      Session session = sessionFactory.openSession();
      Criteria criteria = session.createCriteria(type);
      for (int i = 0; i < params.size(); i++) {
         criteria.add(params.get(i));
      }
      List entities = criteria.list();
      session.close();
      return entities;
   }
}


The test

Code:
public class Test {

   public static void main(String[] args) {
      
      FileSystemXmlApplicationContext applicationContext =
         new FileSystemXmlApplicationContext("/web/WEB-INF/applicationContext.xml");
      
      GenericDAO dao = (GenericDAO) applicationContext.getBean("estadoDAO");
      
      SessionFactory sessionFactory =
         (SessionFactory) applicationContext.getBean("sessionFactory");
      
      Session session = sessionFactory.openSession();
      
      Criterion criterion = Restrictions.like("nome", "RIO", MatchMode.START);
      ArrayList<Criterion> params = new ArrayList<Criterion>();
      params.add(criterion);
      
      Criteria criteria = session.createCriteria(Estado.class);
      for (int i = 0; i < params.size(); i++) {
         criteria.add(params.get(i));
      }
      
      for (int i = 0; i < criteria.list().size(); i++) {
         Estado estado = (Estado) criteria.list().get(i);
         System.out.println(estado.getNome());
      }
   }

}


I got no erros, but the criteria.list() list is always empty. Any ideas why this is happening?
Thanks for the attention!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.