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