Quantcast
Channel: Query – Technical Blog
Viewing all articles
Browse latest Browse all 8

Hibernate custom Criteria : check the length of string properties

$
0
0

There is no in-built function to fetch string properties length in hibernate criteria. However Hibernate also allows the creation of a custom Criterion that can be used in the criteria.

Like,


select * from tbl where length(ENTITY) = 7;

I created a simple Criterion class that is used to check the length of string properties.

 

@SuppressWarnings("serial")
public class LengthExpression implements Criterion {
	private final String propertyName;
	private final int value;

	public LengthExpression(final String propertyName, final int value) {
		this.propertyName = propertyName;
		this.value = value;
	}

	@Override
	public String toSqlString(final Criteria criteria,
			final CriteriaQuery criteriaQuery) throws HibernateException {
		criteriaQuery.getFactory().getDialect();
		final String[] columns = criteriaQuery.getColumnsUsingProjection(
				criteria, this.propertyName);
		final String queryFragment = "length(" + columns[0] + ") = ?";
		return queryFragment;
	}

	@Override
	public TypedValue[] getTypedValues(final Criteria criteria,
			final CriteriaQuery criteriaQuery) throws HibernateException {
		// An ordered pair of a value and its Hibernate type
		return new TypedValue[] { new TypedValue(Hibernate.INTEGER,
				Integer.valueOf(value), EntityMode.POJO) };
	}
}

In the above code:

The class implements the Criterion Interface which exposes two methods.
The toSqlString() method is what generates the actual sql fragment. As we work with prepared statements, “?” ares used. In this case the sql length function is used. The alias used for property in the actual query is obtained using the getColumnsUsingProjection() method.
The getTypedValues() method tells Hibernate about the data Type to be used in the query. In this case the parameter will be set using the preparedStatement.setInt() method.

To test the code, I used it in a criteria example:

public static void testCustomCriterion() {
		final Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Entity.class);
		criteria.add(new LengthExpression("name", 7));
		List entities = criteria.list();
		System.out.println(entities);
	}

The result of the execution in hibernate is below:

    /* criteria query */
    select
        this_.ID as ID0_0_,
        this_.NAME as NAME0_0
    from
        ENTITY this_
    where
        length(this_.NAME) = ?

Viewing all articles
Browse latest Browse all 8

Trending Articles