Hibernate – How to set up the biggest result in DetachedCriteria?

I am using DetachedCriteria, I only want the first result of the query, so I want to do something like LIMIT in DetachedCriteria 1. When I search Google, I found setMaxResult but It’s standard.

How do I do this in DetachedCriteria?

I am also just studying this problem. I don’t like having to use standard solutions because DetachedCriteria The whole purpose of is to define it when you don’t have a Session yet.
In the application I’m working on, this is not an option either, because the place where DetachedCriteria is created is far from where it’s actually executed.

Anyway, I found this clever trick to be able to define limits when creating DetachedCriteria. DetachedCriteria uses Criterion instances, but Criterion uses actual Criteria (and CriteriaQuery) callbacks, so you can set maxResults at this time.

The only problem is that Hibernate has built the sql string and has added a “and” to the query string because it is expecting a comparison statement. But we can fake this by returning ‘1 = 1’.

Please refer to the implementation of LimitBy Criterion below.

public class Limit {
private Limit() {}

public static LimitBy by(int maxResults) {
return new LimitBy(maxResults);
}

static class LimitBy implements Criterion {
private int max;< br />
public LimitBy(int max) {
this.max = max;
}

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
criteria.setMaxResults(max);
return "1 = 1";
}

@Override
public TypedValue[] getTypedValues( Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[0];
}
}
}

So, using this LimitBy class, now You can use the limit DetachedCriteria

DetachedCriteria.forClass(..)
.add(Limit.by(1));

Now, this Only the first result of the query is returned.

This is for Postgres and has not been tested on other databases, but I hope it will also work for other databases. Response when a certain database does not work .

I am using DetachedCriteria, I only want the first result of the query, so I want to do something like LIMIT in DetachedCriteria 1. When I search Google, I found setMaxResult but it’s standard.

How do I do this in DetachedCriteria?

I am also just studying this problem. I don’t like having to use a standard solution, because the whole purpose of DetachedCriteria is to define it when you don’t have a Session yet.
In the application I am working on, this is not an option either, because the place where DetachedCriteria is created is far from where it is actually executed.

Anyway, I found this clever trick It is possible to define restrictions when creating DetachedCriteria. DetachedCriteria uses Criterion instances, but Criterion uses actual Criteria (and CriteriaQuery) callbacks, so you can set maxResults at this time.

The only problem is that Hibernate has already built sql String, and a “and” has been added to the query string because it is expecting a comparison statement. But we can fake this by returning ‘1 = 1’.

See LimitBy below Criterion implementation.

public class Limit {
private Limit() {}

public static LimitBy by(int maxResults) {< br /> return new LimitBy(maxResults);
}

static class LimitBy implements Criterion {
private int max;

public LimitBy(int max ) {
this.max = max;
}

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
criteria .setMaxResults(max);
return "1 = 1";
}

@Override
public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
return new TypedValue[0];
}
}
}

So, using this LimitBy class, you can now use the limit DetachedCriteria

DetachedCriteria.forClass(..)
.add(Limit.by(1));

Now, this only returns the first result of the query .

This is for Postgres and has not been tested on other databases, but I hope it will also work for other databases. The response when a certain database does not work.

Leave a Comment

Your email address will not be published.