-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with createQuery and createSQLQuery methods
PostPosted: Thu Aug 21, 2008 12:39 pm 
Newbie

Joined: Thu Aug 21, 2008 12:15 pm
Posts: 5
Hi,
I am using Hibernate3.2.6.
MySQL database 5.0.51
MySQL connector 5.0.4.
JDK1.6.06.

The following is my project dir structure:

Sample:
src:
com/icalib/ocs/server
TechnologyArea.java
TechAreaApp.java
TechnologyArea.hbm.xml

under src/ i have hibernate.cfg.xml

The contents of hibernate.cfg.xml is:
Code:
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="show_sql">true</property>

    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>

    <!-- Mapping files will go here.... -->
    <mapping resource="com/icalib/ocs/server/TechnologyArea.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
********************************************
The contents of TechnologyArea.hbm.xml is:
Code:
<hibernate-mapping>
  <class name="com.icalib.ocs.server.TechnologyArea" table="techarea">
    <id name="technologyId" column="id" type="java.lang.Integer">
      <generator class="increment"/>
    </id>
    <property name="technologyName" />
  </class>
</hibernate-mapping>
***********************************************
TechAreaApp is where the error occurs:
I have two methods getAll() and main()

here it is:

getAll():

Code:
public List<TechnologyArea> getAll(){
      Session session = HibernateUtil.getSession();
      
      session.beginTransaction();
      
      List<TechnologyArea> taList = session.createQuery("select h from techarea as h").list();
      
      session.getTransaction().commit();
      
      return taList;
   }

main():

Code:
public static void main(String[] args) {
      // TODO Auto-generated method stub
      TechAreaApp taApp = new TechAreaApp();
      List<TechnologyArea> list = taApp.getAll();
      Iterator<TechnologyArea> itr = list.iterator();
      while(itr.hasNext()){
         TechnologyArea ta = itr.next();
         System.out.println("TA "+ ta);
      }
   }

**************************
When I run the above code I get the following exception:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: techarea is not mapped [select h from techarea as h] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
...............
..........................

When I change createQuery() to createSQLQuery("select * from techarea") I get the following exception:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.icalib.ocs.server.TechnologyArea
at com.icalib.ocs.server.TechAreaApp.main(TechAreaApp.java:32)



I am unable to solve this. I have added all the jar files from the hibernate 3.2.6 distribution...but I keep getting these two errors.

Thanks for any help in advance.

Gavin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 2:20 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
With hql use your class name:
Code:
from com.icalib.ocs.server.TechnologyArea


Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 2:55 pm 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
You must use the name of the class, not table name

Code:
List<TechnologyArea> taList = session.createQuery("from TechnologyArea").list();

_________________
Please don't forget to rateME!


Last edited by passos on Thu Aug 21, 2008 11:01 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 10:32 pm 
Newbie

Joined: Thu Aug 21, 2008 12:15 pm
Posts: 5
THANK you sooo much Passos and Croudet

But is there any explanation for the ClassCastException when using createSQLQuery("select * from techarea").

Why is the List holding Object objects even when I am using generics?

Thanks once again

Gavin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 11:00 pm 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
If you use SQLQuery, use table in the query, set the alias for table and set this alias to entity

Code:
List<TechnologyArea> taList = session.createSQLQuery("select {t.*} from techarea t")
.addEntity("t", TechnologyArea.class)
.list();

_________________
Please don't forget to rateME!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 1:34 am 
Newbie

Joined: Thu Aug 21, 2008 12:15 pm
Posts: 5
Thank you Passos once again.

Gavin


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

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.