-->
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.  [ 9 posts ] 
Author Message
 Post subject: HQL question
PostPosted: Fri May 25, 2007 9:41 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Hello,

I want to make a query which returns an object of type ImportedGeometries. The query is based on three parameters:
- name
- version
- country

Here is the source of this class:

Code:
public class ImportedGeometries {
   
   private Long id;
   
   // name of the referenced geometry table in the db
   private String name;   
   private String version;   
   private Date importDate;   
   private Timestamp importTime;   
   private Vendor vendor;   
   private User user;   
   private Country country;   
   private Statistic statistic;   
   private Set logEntries = new HashSet();

        //Getters, Setters


Here is the mapping file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.mycompany.transferobjects.ImportedGeometries" table="tbl_imported_geometries" lazy="false">
   <id name="id" type="java.lang.Long">
      <column name="id_imported_geometries" />
      <generator class="native"/>
   </id>
   <property name="name">
      <column name="name" />
    </property>
   <property name="version">
      <column name="version" />
    </property>
   <property name="importDate">
      <column name="import_date" />
    </property>
    <property name="importTime">
       <column name="import_time" />
    </property>
    <many-to-one name="vendor" class="com.mycompany.transferobjects.Vendor" cascade="all" unique="true" column="fk_vendor"/>
    <many-to-one name="user" class="com.mycompany.transferobjects.User" cascade="all" unique="true" column="fk_user"/>
    <many-to-one name="country" class="com.mycompany.transferobjects.Country" cascade="all" unique="true" column="fk_country"/>
    <many-to-one name="statistic" class="com.mycompany.transferobjects.Statistic" cascade="all" unique="true" column="fk_statistic"/>
    <set name="logEntries" cascade="all" lazy="false">
      <key column="fk_imported_geometries"/>
      <one-to-many class="com.mycompany.transferobjects.Log"/>
    </set>
  </class>
</hibernate-mapping>



My query-method looks like this:

Code:
public ImportedGeometries getImportedGeometries(String name, String version, Country country) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      
      session.beginTransaction();
      
      ImportedGeometries i = (ImportedGeometries) session.createQuery("select i from ImportedGeometries i where i.name = :tblname and i.version = :vers and i.country = :countr")
         .setParameter("tblname", name)
         .setParameter("vers", version)
         .setParameter("countr", country);
      
      session.getTransaction().commit();
      
      return i;
   }


Here is the log from Hibernate:

Code:
15:26:25,308 DEBUG AST:266 - --- HQL AST ---
\-[QUERY] 'query'
    +-[SELECT_FROM] 'SELECT_FROM'
    |  +-[FROM] 'from'
    |  |  \-[RANGE] 'RANGE'
    |  |     +-[DOT] '.'
    |  |     |  +-[DOT] '.'
    |  |     |  |  +-[DOT] '.'
    |  |     |  |  |  +-[DOT] '.'
    |  |     |  |  |  |  +-[DOT] '.'
    |  |     |  |  |  |  |  +-[IDENT] 'com'
    |  |     |  |  |  |  |  \-[IDENT] 'ptvag'
    |  |     |  |  |  |  \-[IDENT] 'geomanagement'
    |  |     |  |  |  \-[IDENT] 'dao'
    |  |     |  |  \-[IDENT] 'transferobjects'
    |  |     |  \-[IDENT] 'ImportedGeometries'
    |  |     \-[ALIAS] 'i'
    |  \-[SELECT] 'select'
    |     \-[IDENT] 'i'
    \-[WHERE] 'where'
       \-[AND] 'and'
          +-[AND] 'and'
          |  +-[EQ] '='
          |  |  +-[DOT] '.'
          |  |  |  +-[IDENT] 'i'
          |  |  |  \-[IDENT] 'name'
          |  |  \-[COLON] ':'
          |  |     \-[IDENT] 'tblname'
          |  \-[EQ] '='
          |     +-[DOT] '.'
          |     |  +-[IDENT] 'i'
          |     |  \-[IDENT] 'version'
          |     \-[COLON] ':'
          |        \-[IDENT] 'vers'
          \-[EQ] '='
             +-[DOT] '.'
             |  +-[IDENT] 'i'
             |  \-[IDENT] 'country'
             \-[COLON] ':'
                \-[IDENT] 'countr'

15:26:25,318 DEBUG ErrorCounter:68 - throwQueryException() : no errors
15:26:25,318 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]
15:26:25,328 DEBUG FromElement:108 - FromClause{level=1} :  com.mycompany.dao.transferobjects.ImportedGeometries (i) -> importedge0_
15:26:25,328 DEBUG FromReferenceNode:51 - Resolved :  i -> importedge0_.id_imported_geometries
15:26:25,338 DEBUG FromReferenceNode:51 - Resolved :  i -> importedge0_.id_imported_geometries
15:26:25,338 DEBUG FromElement:525 - handling property dereference [com.mycompany.dao.transferobjects.ImportedGeometries (i) -> name (class)]
15:26:25,348 DEBUG DotNode:555 - getDataType() : name -> org.hibernate.type.StringType@1422d0b
15:26:25,348 DEBUG FromReferenceNode:51 - Resolved :  i.name -> importedge0_.name
15:26:25,358 DEBUG FromReferenceNode:51 - Resolved :  i -> importedge0_.id_imported_geometries
15:26:25,358 DEBUG FromElement:525 - handling property dereference [com.mycompany.dao.transferobjects.ImportedGeometries (i) -> version (class)]
15:26:25,368 DEBUG DotNode:555 - getDataType() : version -> org.hibernate.type.StringType@1422d0b
15:26:25,368 DEBUG FromReferenceNode:51 - Resolved :  i.version -> importedge0_.version
15:26:25,378 DEBUG FromReferenceNode:51 - Resolved :  i -> importedge0_.id_imported_geometries
15:26:25,388 DEBUG FromElement:525 - handling property dereference [com.mycompany.dao.transferobjects.ImportedGeometries (i) -> country (class)]
15:26:25,388 DEBUG DotNode:555 - getDataType() : country -> org.hibernate.type.ManyToOneType(com.mycompany.dao.transferobjects.Country)
15:26:25,388 DEBUG DotNode:512 - dereferenceShortcut() : property country in com.mycompany.dao.transferobjects.ImportedGeometries does not require a join.
15:26:25,398 DEBUG DotNode:541 - terminal propertyPath = [country]
15:26:25,398 DEBUG FromReferenceNode:51 - Resolved :  i.country -> importedge0_.fk_country
15:26:25,408 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=1, statement=select]
15:26:25,408 DEBUG HqlSqlWalker:509 - processQuery() :  ( SELECT ( {select clause} importedge0_.id_imported_geometries ) ( FromClause{level=1} tbl_imported_geometries importedge0_ ) ( where ( and ( and ( = ( importedge0_.name importedge0_.id_imported_geometries name ) ? ) ( = ( importedge0_.version importedge0_.id_imported_geometries version ) ? ) ) ( = ( importedge0_.fk_country importedge0_.id_imported_geometries country ) ? ) ) ) )
15:26:25,418 DEBUG JoinProcessor:148 - Using FROM fragment [tbl_imported_geometries importedge0_]
15:26:25,418 DEBUG HqlSqlBaseWalker:123 - select >> end [level=1, statement=select]
15:26:25,448 DEBUG AST:232 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT'  querySpaces (tbl_imported_geometries)
    +-[SELECT_CLAUSE] SelectClause: '{select clause}'
    |  +-[ALIAS_REF] IdentNode: 'importedge0_.id_imported_geometries as id1_3_' {alias=i, className=com.mycompany.dao.transferobjects.ImportedGeometries, tableAlias=importedge0_}
    |  \-[SQL_TOKEN] SqlFragment: 'importedge0_.name as name3_, importedge0_.version as version3_, importedge0_.import_date as import4_3_, importedge0_.import_time as import5_3_, importedge0_.fk_vendor as fk6_3_, importedge0_.fk_user as fk7_3_, importedge0_.fk_country as fk8_3_, importedge0_.fk_statistic as fk9_3_'
    +-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[i], fromElementByTableAlias=[importedge0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
    |  \-[FROM_FRAGMENT] FromElement: 'tbl_imported_geometries importedge0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=i,role=null,tableName=tbl_imported_geometries,tableAlias=importedge0_,origin=null,colums={,className=com.mycompany.dao.transferobjects.ImportedGeometries}}
    \-[WHERE] SqlNode: 'where'
       \-[AND] SqlNode: 'and'
          +-[AND] SqlNode: 'and'
          |  +-[EQ] BinaryLogicOperatorNode: '='
          |  |  +-[DOT] DotNode: 'importedge0_.name' {propertyName=name,dereferenceType=4,propertyPath=name,path=i.name,tableAlias=importedge0_,className=com.mycompany.dao.transferobjects.ImportedGeometries,classAlias=i}
          |  |  |  +-[ALIAS_REF] IdentNode: 'importedge0_.id_imported_geometries' {alias=i, className=com.mycompany.dao.transferobjects.ImportedGeometries, tableAlias=importedge0_}
          |  |  |  \-[IDENT] IdentNode: 'name' {originalText=name}
          |  |  \-[NAMED_PARAM] ParameterNode: '?' {name=tblname, expectedType=org.hibernate.type.StringType@1422d0b}
          |  \-[EQ] BinaryLogicOperatorNode: '='
          |     +-[DOT] DotNode: 'importedge0_.version' {propertyName=version,dereferenceType=4,propertyPath=version,path=i.version,tableAlias=importedge0_,className=com.mycompany.dao.transferobjects.ImportedGeometries,classAlias=i}
          |     |  +-[ALIAS_REF] IdentNode: 'importedge0_.id_imported_geometries' {alias=i, className=com.mycompany.dao.transferobjects.ImportedGeometries, tableAlias=importedge0_}
          |     |  \-[IDENT] IdentNode: 'version' {originalText=version}
          |     \-[NAMED_PARAM] ParameterNode: '?' {name=vers, expectedType=org.hibernate.type.StringType@1422d0b}
          \-[EQ] BinaryLogicOperatorNode: '='
             +-[DOT] DotNode: 'importedge0_.fk_country' {propertyName=country,dereferenceType=ROOT_LEVEL,propertyPath=country,path=i.country,tableAlias=importedge0_,className=com.mycompany.dao.transferobjects.ImportedGeometries,classAlias=i}
             |  +-[ALIAS_REF] IdentNode: 'importedge0_.id_imported_geometries' {alias=i, className=com.mycompany.dao.transferobjects.ImportedGeometries, tableAlias=importedge0_}
             |  \-[IDENT] IdentNode: 'country' {originalText=country}
             \-[NAMED_PARAM] ParameterNode: '?' {name=countr, expectedType=org.hibernate.type.ManyToOneType(com.mycompany.dao.transferobjects.Country)}

15:26:25,458 DEBUG ErrorCounter:68 - throwQueryException() : no errors
15:26:25,458 DEBUG QueryTranslatorImpl:216 - HQL: select i from com.mycompany.dao.transferobjects.ImportedGeometries i where i.name = :tblname and i.version = :vers and i.country = :countr
15:26:25,458 DEBUG QueryTranslatorImpl:217 - SQL: select importedge0_.id_imported_geometries as id1_3_, importedge0_.name as name3_, importedge0_.version as version3_, importedge0_.import_date as import4_3_, importedge0_.import_time as import5_3_, importedge0_.fk_vendor as fk6_3_, importedge0_.fk_user as fk7_3_, importedge0_.fk_country as fk8_3_, importedge0_.fk_statistic as fk9_3_ from tbl_imported_geometries importedge0_ where importedge0_.name=? and importedge0_.version=? and importedge0_.fk_country=?
15:26:25,488 DEBUG ErrorCounter:68 - throwQueryException() : no errors
15:26:25,498 DEBUG HQLQueryPlan:269 - HQL param location recognition took 0 mills (select i from ImportedGeometries i where i.name = :tblname and i.version = :vers and i.country = :countr)
java.lang.ClassCastException
   at com.mycompany.dao.ImportedGeometriesDAOImpl.getImportedGeometries(ImportedGeometriesDAOImpl.java:54)
   at com.mycompany.dao.jdbc.Workflow2Handler.writeStatistic(Workflow2Handler.java:110)
   at com.mycompany.dao.jdbc.Workflow2Handler.processWorkflow2(Workflow2Handler.java:83)
   at com.mycompany.servlets.ShapeImportServlet$Workflow2Thread.startWorkflow2(ShapeImportServlet.java:238)
   at com.mycompany.servlets.ShapeImportServlet$Workflow2Thread.run(ShapeImportServlet.java:243)



I hope someone can help me to find the bug. Maybe you are not allowed to use other than the standard Java datatypes? If so, how can I get to the needed result in another way?

Regards,
TMK[/code]


Last edited by tmk on Fri May 25, 2007 10:11 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 9:55 am 
Newbie

Joined: Tue Feb 27, 2007 6:07 am
Posts: 5
Could you please describe what the problem is?
Oh, and do you have a reason for using hibernate mapping files instead of annotations? Annotations involve a lot less writing, and they're easier to keep in sync with your code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:01 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Look at the exception messages, the problem is that I am not able to get a ImportedGeometries object with this HQL query.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:03 am 
Newbie

Joined: Tue Feb 27, 2007 6:07 am
Posts: 5
Code:
ErrorCounter:68 - throwQueryException() : no errors

This says no errors? What exception are you getting in the actual program?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:12 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
alexg79 wrote:
Code:
ErrorCounter:68 - throwQueryException() : no errors

This says no errors? What exception are you getting in the actual program?


Sorry, I forgot to post the whole Hibernate log in my first post.
Now I added it --> look above.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:15 am 
Newbie

Joined: Tue Feb 27, 2007 6:07 am
Posts: 5
Could you please show the code that executes the query? That might help explain the ClassCastException.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:16 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
It is the method from my posting above:

Code:
public ImportedGeometries getImportedGeometries(String name, String version, Country country) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
     
      session.beginTransaction();
     
      ImportedGeometries i = (ImportedGeometries) session.createQuery("select i from ImportedGeometries i where i.name = :tblname and i.version = :vers and i.country = :countr")
         .setParameter("tblname", name)
         .setParameter("vers", version)
         .setParameter("countr", country);
     
      session.getTransaction().commit();
     
      return i;
   }



Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:19 am 
Newbie

Joined: Tue Feb 27, 2007 6:07 am
Posts: 5
This is a piece of cake.
You're trying to cast a Query object to ImportedGeometries. You must add ".uniqueResult()" to the end to get the ImportedGeometries object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 10:25 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
alexg79 wrote:
This is a piece of cake.
You're trying to cast a Query object to ImportedGeometries. You must add ".uniqueResult()" to the end to get the ImportedGeometries object.


Thank you very much, I have totally overseen this point... You opened my eyes. I think it is time to leave the office now :)

Bye,
TMK


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.