-->
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: hql select statement error, can you take a peek.
PostPosted: Mon Dec 11, 2006 3:39 pm 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
Could someone more skilled in HQL please take a look at my query.. I am doing something wrong and after fooling with it for more time than I have, I still do not have it working..
I know that it is something simple, but once again inexperience is proving to be a horrible thing..

here is my HQL statement which you can see in the following mapping files:

Code:
select details.metricName from com.pilotsoftware.metricsmanager.shared.hibernate.pojo.ContentMaster contMast left join fetch contMast.details details where contMast.masterId = :masterId

If I get rid of the “select details.metricName” everything works great.
With the Select statement this is the Query that hibernate generates..

Hibernate:
select
contentmas0_.master_id as master1_0_0_,
details1_.detail_id as detail1_1_1_,
contentmas0_.name as name0_0_,
details1_.detail_id as detail1_0__
from
content_master contentmas0_
left outer join
content_detail details1_
on contentmas0_.master_id=details1_.master_id
where
contentmas0_.master_id=?

So something is wrong with the select.. But I don’t see what.. Can you me out..

ray




Hibernate version: 3

Mapping documents:
Master record htm.xml file. This contains the query (findContentDetailMetricNamesForMaster) that is giving me problems.
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 package="com.pilotsoftware.metricsmanager.shared.hibernate.pojo">
    <class name="ContentMaster" table="content_master">
        <id name="masterId" type="int" column="master_id">
            <!-- <column name="master_id" /> -->
            <!--<generator class="sequence"/>-->
            <generator class="native">
                <param name="native">masterId</param>
            </generator>
        </id>
 
        <property name="name" type="string">
            <column name="name" not-null="true" />
        </property>
     
       <set
           name="details"
           table="content_detail"
           cascade="all-delete-orphan">
           <key column="master_id"/>
           <one-to-many class="ContentDetail"/>
        </set>
    </class>
   
   [b]<query name="findContentDetailMetricNamesForMaster">[/b]
        <![CDATA[
select details.metricName from com.pilotsoftware.metricsmanager.shared.hibernate.pojo.ContentMaster contMast left join fetch contMast.details details where contMast.masterId = :mastered
]]>
    </query>   
   
</hibernate-mapping>


This is the Details htm.xml 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 package="com.pilotsoftware.metricsmanager.shared.hibernate.pojo">

   <class name="ContentDetail" table="content_detail">
        <id name="detailId" column="detail_id" type="java.lang.Integer">
            <generator class="native">
                <param name="native">detailId</param>
            </generator>
        </id>
      <!--  dim5Member does not require an Alis in the query -->
       
        <property name="metricName" column="metric_name" type="java.lang.String" />
              
        <many-to-one name="contentMaster" column="master_id" class="ContentMaster"  not-null="true" />

       
    </class>
   
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():
Code:
HQLQueryTester hqlQueryTester = new HQLQueryTester();
Session session = HibernateSessionFactory.currentSession();
Map nameValueMap = new HashMap();
nameValueMap.put("masterId", new Integer(1));
hqlQueryTester.executeNamedQuery(session, "findContentDetailMetricNamesForMaster", nameValueMap);
session.close();

----
Code:
public List executeNamedQuery(Session session, String hqlQueryName, Map nameValueMap){
    Transaction transaction = session.beginTransaction();
    Query query = session.getNamedQuery(hqlQueryName);
    List results = bindParameters(query, nameValueMap).list();
    transaction.commit();
    return results;
}

----
Code:
private Query bindParameters(Query query, Map nameValueMap){
    Map.Entry entry = null;
    Iterator nameValueEtries = nameValueMap.entrySet().iterator();
    while (nameValueEtries.hasNext()) {
       entry = (Map.Entry)nameValueEtries.next();
       // CURRENTLY ONLY STRING and INTEGER VALUES ARE SUPPORTED
       if (entry.getValue() instanceof String) {
          query.setString((String)entry.getKey(), (String)entry.getValue());
       } else {
          query.setInteger((String)entry.getKey(), (Integer)entry.getValue());
       }
    }
    return query;
}


Full stack trace of any exception that occurs:
org.hibernate.HibernateException: Errors in named queries: findContentDetailMetricNamesForMaster
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:339)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at com.pilotsoftware.metricsmanager.server.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:53)
at Driver.main(Driver.java:17)
Exception in thread "main" java.lang.NullPointerException
at com.pilotsoftware.metricsmanager.server.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:60)
at Driver.main(Driver.java:17)


Name and version of the database you are using:
Postgres 8.2.0.1

The generated SQL (show_sql=true):
Hibernate:
Code:
    select
        contentmas0_.master_id as master1_0_0_,
        details1_.detail_id as detail1_1_1_,
        contentmas0_.name as name0_0_,
        details1_.detail_id as detail1_0__
    from
        content_master contentmas0_
    left outer join
        content_detail details1_
            on contentmas0_.master_id=details1_.master_id
    where
        contentmas0_.master_id=?

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?
no, seems to work great if I remove the select clause on the HQL.

Read this: http://hibernate.org/42.html
okay


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 4:48 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I think the left join fetch might be your problem. In the Hibernate docs it says about join fetching:

Quote:
A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). Also, the associated objects are not returned directly in the query results. Instead, they may be accessed via the parent object. The only reason we might need an alias is if we are recursively join fetching a further collection:

(http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-joins)

So I think it's wrong to use
Code:
details.metricName
in the select clause if you are using fetch in the join clause:
Code:
...left join fetch contMast.details details...

_________________
nathan


Top
 Profile  
 
 Post subject: ah, well that makes sense, but..
PostPosted: Mon Dec 11, 2006 5:12 pm 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
oh.. so this makes sense.. Thanks.. So if I just want a list of one field out of the queried objects, in this case the names of the details, then I would have to write a chunk of code that would iterate through all the details and pull off their names. I can do that. but it would be nice to just have the database return only the names.. Performance, you see. So is there a way to do this in HQL?

see this is also about learning HQL stuff as I am doing this work. Is there an way to do this..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 5:37 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
did you try just removing the 'fetch' from the query?

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 10:09 am 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
I am a happy guy and learned something which allways makes me happy.. I guess you can teach a good dog new tricks after all.. thanks man.. that worked.. so it seems that fetch is not required and actually was hurting me. So now I need to read more (no surprise there) about what fetch is for and all of that.. I thought that i needed to have it to "fetch/auto-load/pre-load" the details set, but that is not the case at all.. More reading is needed I think.. Hey but thanks man.. this worked great.. Now on to the next thing.. thanks Nathan..

ray


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 10:33 am 
Beginner
Beginner

Joined: Thu Feb 23, 2006 11:22 am
Posts: 30
oh I also boosted you to the magic number 30.. Wow.. I am a 6.. But hey keep studying and answer some questions and I can be a 30 someday also..


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.