0

If i put a <query..> in my Car.hbm.xml the session i get returned from (Session) HibernateUtil.getSessionFactory().getCurrentSession(); is null, if i delete the query from my xml mapping file the session is not null anymore. Why do i get this error? I am really stuck with that kind of problem.

I got this xml mapping:

<hibernate-mapping package="at.opendata.entitys">      
    <class name="Car" table="Cars">     
        <id name="id" column="car_Id">
            <generator class="increment"/>
        </id>
        <set name="carDetails" table="Cardetail" cascade="all" lazy="false" fetch="select" >
            <key column="car_id" not-null="true"/>
            <one-to-many class="CarDetail" />
        </set>  
        <property name="name" not-null="true"/>
        <property name="vin" not-null="true"/>              
    </class>    

    <query name="dailysales">
        <![CDATA[select sum(date_part('minute', age(cd.gone, cd.back))) from Car as c left join c.carDetails as cd where cd.gone is not null and cd.back is not null]]>
    </query>
</hibernate-mapping>

And this is where i want to call the named query:

Transaction transaction = null;
int amount = 0;

try{
    Session session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();  
    transaction = session.beginTransaction();

    amount = (Integer)session.getNamedQuery("dailysales").uniqueResult();
krackmoe
  • 1,743
  • 7
  • 29
  • 53

1 Answers1

0

Query is not valid HQL query. It seems to contain functions (date_part, age) specific to SQL dialect of chosen database. One option is to use named SQL query instead. Other option is to extend dialect to contain those functions.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • Mh... i added already this dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect But mh.. in my sql query i have to specify which object i want to get returned then. I have cars, and i want to sum up over all cars the daily sales sum. This has nothing to do with one car.. but i dont have another entity for this... i cant return a car then.. mhh.. – krackmoe Feb 18 '13 at 19:10