-->
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: HQL error - count() using find method
PostPosted: Tue Jun 04, 2013 2:37 pm 
Newbie

Joined: Tue Jun 04, 2013 2:27 pm
Posts: 1
I am using Spring with Hibernate, and trying to get the number of records count in a table using the following code:

hibernateTemplate.find("select count(con.CONTACT_ID) from Contact con")

I have also tried con.id instead of con.CONTACT_ID but same error.

Here is the error:

org.springframework.orm.hibernate3.HibernateQueryException: ClassNotFoundException:
org.hibernate.hql.ast.HqlToken [select count(con.CONTACT_ID) from br.com.braziljs.loiane.model.Contact con]; nested exception is org.hibernate.QueryException:
ClassNotFoundException: org.hibernate.hql.ast.HqlToken [select count(con.CONTACT_ID) from br.com.braziljs.loiane.model.Contact con]

I am having problem only with the above statement which is being used to display the records on the starting view page. The 'Add', 'Update' and 'Delete' are working fine.

The generated SQL (show_sql=true):
Hibernate: select top 35 this_.CONTACT_ID as CONTACT1_0_0_, this_.CONTACT_EMAIL as CONTACT2_0_0_, this_.CONTACT_NAME as CONTACT3_0_0_, this_.CONTACT_PHONE as CONTACT4_0_0_ from CONTACT this_|#]

Not sure why there is this_ in the generated Sql instead of the database table name.

Here is my code.

db-config.xml
-------------

Code:
   <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="jdbc/meSpObjReqSystem"/>
        <property name="lookupOnStartup" value="true"/>
        <property name="cache" value="true" />
   </bean>
   
     <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <property name="packagesToScan" value="br.com.braziljs.loiane.model" />
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
       <prop key="hibernate.generate_statistics">true</prop>
        </props>
        </property>
      <property name="annotatedClasses">
       <list>
         <value>br.com.braziljs.loiane.model.Contact</value>
       </list>
      </property>      
    </bean>
   
   <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
   <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>



Contact.java
------------

Code:
package br.com.braziljs.loiane.model;

@JsonAutoDetect
@Entity
@Table(name="CONTACT")
public class Contact {
   
   @Id
   @GeneratedValue
   @Column(name="CONTACT_ID")
   private int id;
   
   @Column(name="CONTACT_NAME", nullable=false)
   private String name;
   
   @Column(name="CONTACT_PHONE", nullable=false)
   private String phone;
   
   @Column(name="CONTACT_EMAIL", nullable=false)
   private String email;
   
   public int getId() {
      return id;
   }
   
   public void setId(int id) {
      this.id = id;
   }
   
   public String getName() {
      return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }
   
   public String getPhone() {
      return phone;
   }
   
   public void setPhone(String phone) {
      this.phone = phone;
   }
   
   public String getEmail() {
      return email;
   }
   
   public void setEmail(String email) {
      this.email = email;
   }
   
}


ContactDAO.java
---------------

Code:
@Repository
public class ContactDAO {
   
   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory) {
      hibernateTemplate = new HibernateTemplate(sessionFactory);
   }
   
   /**
    * Get List of contacts from database
    * @return list of all contacts
    */
   @SuppressWarnings("unchecked")
   public List<Contact> getContacts(int start, int limit) {
      
      DetachedCriteria criteria = DetachedCriteria.forClass(Contact.class);

      return hibernateTemplate.findByCriteria(criteria, start, limit);
   }

   /**
    * Delete a contact with the id passed as parameter
    * @param id
    */
   public void deleteContact(int id){
      Object record = hibernateTemplate.load(Contact.class, id);
      hibernateTemplate.delete(record);
   }
   
   /**
    * Create a new Contact on the database or
    * Update contact
    * @param contact
    * @return contact added or updated in DB
    */
   public Contact saveContact(Contact contact){
      hibernateTemplate.saveOrUpdate(contact);
      return contact;
   }
   
   /**
    * Get total of Contacts from database
    * @return
    */
   public int getTotalContacts(){
      return DataAccessUtils.intResult(hibernateTemplate.find("select count(con.id) from Contact con"));
      //return 6; //this works fine
   }

}


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.