-->
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.  [ 3 posts ] 
Author Message
 Post subject: Inner select using Criteria API
PostPosted: Thu Jan 24, 2008 11:27 am 
Newbie

Joined: Thu Jan 24, 2008 11:18 am
Posts: 1
Hello Hibernate support,

we would like to translate this SQL call:

Code:
SELECT FULLJOBNAME, TIMESTAMP, STATUS
FROM   EM_JOBSTATUS as s1
WHERE  FULLJOBNAME LIKE '/jobs/25/%' AND TIMESTAMP=(SELECT MAX(s2.TIMESTAMP)
    FROM EM_JOBSTATUS as s2
    WHERE s1.FULLJOBNAME = s2.FULLJOBNAME)


into Criteria API call.

We are starting with:

Code:
Criteria c =sessionFactory.getCurrentSession().createCriteria(PlainJobStatusBO.class);
c.add(Restrictions.like("fullJobName", jobNameBase+"%"));
c.add(Restrictions.eq("timestamp", ???));


PlainJobStatusBO mapping is:

Code:
<hibernate-mapping>
    <class name="com.service.PlainJobStatusBO" table="EM_JOBSTATUS">
        <id name="id" type="java.lang.Integer" access="field">
            <column name="id" />
            <generator class="native"></generator>
        </id>
        <version name="version" type="java.lang.Integer" access="field">
            <column name="version" />
        </version>
        <property name="fullJobName" type="java.lang.String" access="field">
            <column name="fullJobName" length="64" />
        </property>
        <property name="timestamp" type="java.util.Date" access="field">
            <column name="timestamp" />
        </property>
        <property name="status" type="java.lang.String" access="field">
            <column name="status" length="16" />
        </property>
    </class>
</hibernate-mapping>


Thanks in advance!

Hibernate version: 3.3.0.ga

Name and version of the database you are using: DB2 9.1


Top
 Profile  
 
 Post subject: Re: Inner select using Criteria API
PostPosted: Thu Jan 24, 2008 12:05 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Your only way might be to use Restrictions.sqlRestriction() instread of Restrictions.eq(). Criteria API will take care of translating aliases if you need to use any.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 1:37 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
try to avoid the subquery


Code:
select max(timestamp), fulljobname, status
where .fulljobname like ...
group by fulljobname, status



which I believe would translate into somethng like this, using Projections

Code:
List results = session.createCriteria(PlainJobStatus.class)
    .setProjection( Projections.projectionList()
        .add( Projections.max("timestamp"), "timestamp" )
        .add( Projections.groupProperty("fulljobname"), "fulljobname" )
        .add( Projections.groupProperty("status"), "status" )
    )
    .add( Restrictions. ... )
    .list();

_________________
Gonzalo Díaz


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