alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (OrderByLimitQuery.java)

This example Hibernate source code file (OrderByLimitQuery.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

abstractentitytest, entitymanager, integer, integer, inttestentity, inttestentity, list, list, orderbylimitquery, priority, suppresswarnings, test, test, util

The Hibernate OrderByLimitQuery.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Middleware LLC.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.envers.test.integration.query;

import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.IntTestEntity;
import org.junit.Test;

import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.List;

/**
 * @author Adam Warski (adam at warski dot org)
 */
@SuppressWarnings({"unchecked"})
public class OrderByLimitQuery extends AbstractEntityTest {
    private Integer id1;
    private Integer id2;
    private Integer id3;
    private Integer id4;
    private Integer id5;

    public void configure(Ejb3Configuration cfg) {
        cfg.addAnnotatedClass(IntTestEntity.class);
    }

    @Test
    @Priority(10)
    public void initData() {
        // Revision 1
        EntityManager em = getEntityManager();
        em.getTransaction().begin();

        IntTestEntity ite1 = new IntTestEntity(12);
        IntTestEntity ite2 = new IntTestEntity(5);
        IntTestEntity ite3 = new IntTestEntity(8);
        IntTestEntity ite4 = new IntTestEntity(1);

        em.persist(ite1);
        em.persist(ite2);
        em.persist(ite3);
        em.persist(ite4);

        id1 = ite1.getId();
        id2 = ite2.getId();
        id3 = ite3.getId();
        id4 = ite4.getId();

        em.getTransaction().commit();

        // Revision 2
        em.getTransaction().begin();

        IntTestEntity ite5 = new IntTestEntity(3);
        em.persist(ite5);
        id5 = ite5.getId();

        ite1 = em.find(IntTestEntity.class, id1);
        ite1.setNumber(0);

        ite4 = em.find(IntTestEntity.class, id4);
        ite4.setNumber(15);

        em.getTransaction().commit();
    }

    @Test
    public void testEntitiesOrderLimitByQueryRev1() {
        List res_0_to_1 = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 1)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(0)
                .setMaxResults(2)
                .getResultList();

        List res_2_to_3 = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 1)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(2)
                .setMaxResults(2)
                .getResultList();

        List res_empty = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 1)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(4)
                .setMaxResults(2)
                .getResultList();

        assert Arrays.asList(new IntTestEntity(12, id1), new IntTestEntity(8, id3)).equals(res_0_to_1);
        assert Arrays.asList(new IntTestEntity(5, id2), new IntTestEntity(1, id4)).equals(res_2_to_3);
        assert Arrays.asList().equals(res_empty);
    }

    @Test
    public void testEntitiesOrderLimitByQueryRev2() {
        List res_0_to_1 = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 2)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(0)
                .setMaxResults(2)
                .getResultList();

        List res_2_to_3 = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 2)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(2)
                .setMaxResults(2)
                .getResultList();

        List res_4 = getAuditReader().createQuery()
                .forEntitiesAtRevision(IntTestEntity.class, 2)
                .addOrder(AuditEntity.property("number").desc())
                .setFirstResult(4)
                .setMaxResults(2)
                .getResultList();

        assert Arrays.asList(new IntTestEntity(15, id4), new IntTestEntity(8, id3)).equals(res_0_to_1);
        assert Arrays.asList(new IntTestEntity(5, id2), new IntTestEntity(3, id5)).equals(res_2_to_3);
        assert Arrays.asList(new IntTestEntity(0, id1)).equals(res_4);
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate OrderByLimitQuery.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.