-->
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.  [ 1 post ] 
Author Message
 Post subject: Usage of Criteria API
PostPosted: Sun Dec 19, 2004 9:07 am 
Newbie

Joined: Fri Nov 12, 2004 11:08 am
Posts: 7
Location: Amsterdam
Hibernate version:

2.1.6

Mapping documents:

Code:
<hibernate-mapping default-cascade="save-update">
    <class name="com.vvdb.domain.metadata.Resource" table="RESOURCE">
        <!-- A 32 hex character is our surrogate key. It's automatically
        generated by Hibernate with the UUID pattern. -->
        <id name="uniqueId" type="string" unsaved-value="null">
            <column name="uniqueId" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
        <property name="dateCreated"/>
        <property name="dateModified"/>
        <property name="mediaType" type="com.vvdb.domain.metadata.MediaType"/>
        <property name="resourceType" type="com.vvdb.domain.metadata.ResourceType"/>       
        <!-- subclasses -->
        <joined-subclass name="com.vvdb.domain.core.PxResource" table="PXRESOURCE">
            <key column="uniqueId"/>
            <!-- associations -->
            <list name="models" table="MODEL" >
                <key column="pxresource_uniqueId"/>
                <index column="list_index"/>
                <one-to-many class="com.vvdb.domain.model.Model"/>
            </list>
        </joined-subclass>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

I am using the Spring HibernateTemplate:

Code:
public List scroll(final SearchForm values, final int firstElement, final int maxElements) throws DataAccessException {
        logger.info("scroll called");

        List result = getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = getHibernateTemplate().createCriteria(session, PxResource.class)
                                    .createAlias("models", "m");
                if (values.getResourceType() != ResourceType.ANY) {
                    criteria.add(Expression.eq("resourceType", values.getResourceType()));
                }
                if (values.getModelState() != State.STATE0) {
                    criteria.add(Expression.eq("m.modelState", values.getModelState()));
                }
                criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                criteria.addOrder(Order.desc("uniqueId"));
                criteria.setMaxResults(maxElements);
                criteria.setFirstResult(firstElement);
                return criteria.list();
            }
        });
        return result;
    }


Full stack trace of any exception that occurs:

Name and version of the database you are using:

mysql-4.1.7

The generated SQL (show_sql=true):

[junit] 19 Dec 2004 13:42:39,335 INFO HibernatePxResourceDao:79 - scroll called
[junit] Hibernate: select this.uniqueId as uniqueId1_, this.url as url3_1_, this.thumbnail as thumbnail3_1_, this.location as location3_1_, this.a
ctivity as activity3_1_, this.category as category3_1_, this.pxCollection as pxCollec7_3_1_, this.quality as quality3_1_, this.pxResourceType as pxRes
our9_3_1_, this_1_.dateCreated as dateCrea2_2_1_, this_1_.dateModified as dateModi3_2_1_, this_1_.mediaType as mediaType2_1_, this_1_.resourceType as
resource5_2_1_, m.uniqueId as uniqueId0_, m.modelOrigin as modelOri2_1_0_, m.modelState as modelState1_0_, m.modelType as modelType1_0_, m_1_.name as
name0_0_, m_1_.lastname as lastname0_0_ from PXRESOURCE this inner join RESOURCE this_1_ on this.uniqueId=this_1_.uniqueId inner join MODEL m on this.
uniqueId=m.pxresource_uniqueId left outer join CONTRIBUTOR m_1_ on m.uniqueId=m_1_.uniqueId where 1=1 order by this.uniqueId desc limit ?, ?

Debug level Hibernate log excerpt:

Question:

I am trying to implement Pagination using the Criteria API.

When I call the above mentioned scroll method with firstElement=0 and maxElements=25, I want the query to return a List of 25 distinct entities of type PxResource. Instead, it applies the maxElements setting before making the result distinct. The size of the List is therefore unpredictable.

What I would like to accomplish is to apply the maxResults setting AFTER the result has been made distinct. How can I accomplish this? Alternatively maybe somebody can post some example code for accomplishing pagination using the Criteria API.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.