I have a hibernate entity Type like so
@Entity
@Table(name = "type")
public class Type {
@Column(name = "enabled")
private boolean enabled = false;
}
and I want to create a function that returns if any row in the Type table has enabled = true. I want to be as efficient as possible by using a projection or criterion but I don't know where to start
Maybe the most efficient way to do this is to search for the first row that has enabled = true
, and check if it exists.
Criteria crit = session.createCriteria(Type.class);
crit.setProjection(Projections.id());
crit.add(Restrictions.eq("enabled", true));
crit.setMaxResults(1);
boolean anyIsEnabled = crit.uniqueResult() != null;
The query should return result when/if it encounters the first result with enabled = true
, so it doesn't have to go through the whole table like a solution using count
, for example, would have to.