Over a million developers have joined DZone.

Tapestry Grid & Hibernate Criteria

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

Tapestry Grid is a very powerful tool for displaying a list of entities. It expects a GridDataSource as source parameter. A useful tool can be a TypeCoercer which converts a Criteria to GridDataSource.

Here is a simple implementation

public class Criteria2GridDataSourceCoercion implements Coercion<Criteria, GridDataSource> {

    public GridDataSource coerce(final Criteria criteria) {
        return new GridDataSource() {
            private List<?> cache;

            private int startIndex;

            public int getAvailableRows() {
                setupCriteria(Projections.rowCount(), 0, 1);
                long count = ((Long) criteria.uniqueResult());
                return (int) count;

            public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {
                setupCriteria(null, startIndex, endIndex - startIndex + 1);

                if (sortConstraints.size() != 0) {
                    for (SortConstraint sortConstraint : sortConstraints) {
                        String propertyName = sortConstraint.getPropertyModel().getPropertyName();
                                sortConstraint.getColumnSort() == ColumnSort.ASCENDING ?
                                        Order.asc(propertyName) : Order.desc(propertyName));

                cache = criteria.list();
                this.startIndex = startIndex;

            public Object getRowValue (int index) {
                return cache.get(index - startIndex);

            public Class getRowType() {
                if (cache == null) {
                    setupCriteria(null, 0, 1);
                    cache = criteria.list();

                if (cache.size() != 0) {
                    return cache.get(0).getClass();

                return null;

            private void setupCriteria(Projection projection, int firstResult, int maxResults) {

and in your Module class

 public static void addCoercion(Configuration<CoercionTuple> types) {
     types.add(new CoercionTuple(Criteria.class, GridDataSource.class,
             new Criteria2GridDataSourceCoercion()));


public class SamplePage {

    private Session session;

    public Criteria getCriteria() {
        Criteria criteria = session.createCriteria(MyEntity.class);
        return criteria;

<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_3.xsd'>
   <div t:type='Grid' t:source='criteria'>

Dragan came up with another useful Coercer to convert Criteria to Long

public static void contributeTypeCoercer(Configuration configuration) {
    configuration.add(CoercionTuple.create(Criteria.class, Long.class,
            new Coercion() {
                public Long coerce(Criteria input) {
                    input = input.setProjection(Projections.rowCount());
                    input = input.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
                    return (Long) input.uniqueResult();

    configuration.add(CoercionTuple.create(Criteria.class, List.class,
            new Coercion() {
                public List coerce(Criteria input) {
                    return input.list();






The Java Zone is brought to you by Stormpath—a complete, pre-built User Management API. Want to learn how to use JWTs to protect microservices from CSRF and more? Check out this on-demand webinar with our Java Developer Evangelist, Micah Silverman.


Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}