-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem: creating statistics from table. (to_char() problem)
PostPosted: Wed Feb 28, 2007 6:46 am 
Newbie

Joined: Wed Feb 28, 2007 6:06 am
Posts: 3
hello,

I'm a bit new to hibernate,
but trying to manage myself.
got already a lot of my application working,
but i've runned against a wall...

Hope that somebody can help me.

Some info:

Hibernate version: 3.2.2

Mapping documents:
Code:
<hibernate-mapping>
    <class name="be.xxx.model.LoggingLine" table="SXXX_LOGGING_LINE">
        <id name="id" column="LGLN_ID" unsaved-value="0">
            <generator class="increment" />
        </id>
        <many-to-one name="app" class="be.xxx.model.Application" column="APP_ID_FK" not-null="true" />
        <property name="logDate" column="LGLN_TIME_LOG_DATE" not-null="true" />
        <property name="sessionId" column="LGLN_SESSION_ID" not-null="true" />
        <property name="userId" column="LGLN_USER_ID" not-null="false" />
        <property name="url" column="LGLN_URL" not-null="true" />
        <property name="action" column="LGLN_ACTION" not-null="false" />
        <property name="delta" column="LGLN_DELTA" not-null="true" />
    </class>
</hibernate-mapping>
Code:
<hibernate-mapping>
    <class name="be.xxx.model.Application" table="SXXX_APPLICATION">
        <id name="id" column="APP_ID" unsaved-value="0">
            <generator class="increment" />
        </id>
        <property name="name" column="APP_NAME" not-null="true" />
        <property name="code" column="APP_CODE" not-null="true" />
        <property name="type" column="APP_TYPE" not-null="true" />
    </class>
</hibernate-mapping>

Name and version of the database you are using: Oracle 9i

Plain SQL
Code:
SELECT TO_CHAR( LGLN_TIME_LOG_DATE,'YYYYMMDDHH24MI') tijd, count(TO_CHAR( LGLN_TIME_LOG_DATE,'YYYYMMDDHH24MI')) freq
FROM SXXX_LOGGING_LINE
GROUP BY TO_CHAR( LGLN_TIME_LOG_DATE,'YYYYMMDDHH24MI')
ORDER BY TO_CHAR( LGLN_TIME_LOG_DATE,'YYYYMMDDHH24MI') ;

The plain sql code give's what i want...
a count of howmany logginglines there are on that time. (ignoring seconds!!)

that i can't seem to find to get working in hibernate...

what i do already got:
Code:
    public List getCountPerMinute(int applicationId, Date beginDatum, Date eindDatum) {
            String[] id = { "id" };
            Object[] appId = { new Integer(applicationId) };
            List listApp=getHibernateTemplate().findByNamedParam("select a from Application as a where a.id=:id order by a.id", id, appId );
            Application a = (Application)listApp.get(0);
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            String[] app= {"app","bdate","edate"};
            Object[] appApp = {a,df.format(beginDatum),df.format(eindDatum)};
            String sql="SELECT l.logDate, ";
            sql+="COUNT(l.logDate)";
            sql+="FROM LoggingLine as l ";
            sql+="WHERE l.app=:app ";
            sql+="AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') ";
            sql+="GROUP BY l.logDate ";
            sql+="ORDER BY l.logDate ";
            List listCount = getHibernateTemplate().findByNamedParam(sql,app,appApp); 
            return listCount;
    }
But this give's always as count "1" cause there is a diffrent in seconds and miliseconds...

and when i try to replace sql part by:
Code:
            String sql="SELECT to_char(l.logDate,'YYYYMMDDHH24MI') , ";
            sql+="COUNT(to_char(l.logDate,'YYYYMMDDHH24MI')) ";
            sql+="FROM LoggingLine as l ";
            sql+="WHERE l.app=:app ";
            sql+="AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') ";
            sql+="GROUP BY to_char(l.logDate,'YYYYMMDDHH24MI') ";
            sql+="ORDER BY to_char(l.logDate,'YYYYMMDDHH24MI')";

This gives error:
Code:
DEBUG - SessionFactoryUtils.getSession(324) [HTTPThreadGroup-3] | Opening Hibernate session
DEBUG - SessionImpl.<init>(220) [HTTPThreadGroup-3] | opened session at timestamp: 11726591728
DEBUG - QueryPlanCache.getHQLQueryPlan(70) [HTTPThreadGroup-3] | unable to locate HQL query plan in cache; generating (SELECT to_char(l.logDate,'YYYYMMDDHH24MI') , COUNT(to_char(l.logDate,'YYYYMMDDHH24MI')) FROM LoggingLine as l WHERE l.app=:app AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') GROUP BY to_char(l.logDate,'YYYYMMDDHH24MI') ORDER BY to_char(l.logDate,'YYYYMMDDHH24MI'))
DEBUG - QueryTranslatorImpl.compile(214) [HTTPThreadGroup-3] | compiling query
DEBUG - SessionFactoryUtils.doClose(734) [HTTPThreadGroup-3] | Closing Hibernate session
DEBUG - SessionImpl.close(273) [HTTPThreadGroup-3] | closing session
DEBUG - ConnectionManager.cleanup(373) [HTTPThreadGroup-3] | connection already null in cleanup : no action
ERROR - ClickstreamDAOHibernate.getCountPerMinute(217) [HTTPThreadGroup-3] | org.springframework.orm.hibernate3.HibernateQueryException: undefined alias: to_char [SELECT to_char(l.logDate,'YYYYMMDDHH24MI') , COUNT(to_char(l.logDate,'YYYYMMDDHH24MI')) FROM be.xxx.model.LoggingLine as l WHERE l.app=:app AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') GROUP BY to_char(l.logDate,'YYYYMMDDHH24MI') ORDER BY to_char(l.logDate,'YYYYMMDDHH24MI')]; nested exception is org.hibernate.QueryException: undefined alias: to_char [SELECT to_char(l.logDate,'YYYYMMDDHH24MI') , COUNT(to_char(l.logDate,'YYYYMMDDHH24MI')) FROM be.xxx.model.LoggingLine as l WHERE l.app=:app AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') GROUP BY to_char(l.logDate,'YYYYMMDDHH24MI') ORDER BY to_char(l.logDate,'YYYYMMDDHH24MI')]


So i gues there are diffrents reasons...

or.... i'm handling this all wrong... (how to handle it correctly?)
or.... there is a stupid mistake... (how to fix it?)
or.... maybe you tell me?...

thx for your time, and your anwser...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 9:26 am 
Newbie

Joined: Wed Feb 28, 2007 6:06 am
Posts: 3
found it myself, after a long strugle...

Code:
    public List getCountPerMinute(int applicationId, Date beginDatum,
                                  Date eindDatum) {
        Application a = getApplication(applicationId);
        String sql =
            "SELECT to_char(l.logDate,'YYYYMMDDHH24MI') , COUNT(to_char(l.logDate,'YYYYMMDDHH24MI')) FROM LoggingLine as l WHERE l.app=:app AND l.logDate BETWEEN to_date(:bdate,'DD/MM/YYYY HH24:MI:SS') AND to_date(:edate,'DD/MM/YYYY HH24:MI:SS') GROUP BY to_char(l.logDate,'YYYYMMDDHH24MI') ORDER BY to_char(l.logDate,'YYYYMMDDHH24MI')";
        Query query = getQuery(a, sql);
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        query.setParameter("bdate", df.format(beginDatum));
        query.setParameter("edate", df.format(eindDatum));
        return query.list();
    }

    private Query getQuery(Application a, String sql) {
        Session session = this.getSession();
        Query query = session.createQuery(sql);
        query.setCacheable(true);
        query.setParameter("app", a);
        return query;
    }


above is my new code (a bit re-organized)...

the main problem was:
Code:
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
this was set to:
Code:
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>


what i got from the spring live : Quick Start tutorial...

so the main reason was wrong dialect...
as i started using HSQL, but then switched over to Oracle...

so this may be closed...

grtz

D.W.


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