Batakj
Batakj

Reputation: 12743

Getting unique result in Hibernate

how can we get distinct result by using criteria in hibernate.

Upvotes: 9

Views: 20218

Answers (3)

waxwing
waxwing

Reputation: 18743

A more flexible solution may be:

criteria.setProjection(Projections.distinct(Projections.property("property")));

Upvotes: 6

trunkc
trunkc

Reputation: 6293

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

See also https://forum.hibernate.org/viewtopic.php?t=941669

Upvotes: 13

Salandur
Salandur

Reputation: 6453

depends on your query/criteria. if you provide a unique id you can call criteria.uniqueResult() otherwise you call criteria.setMaxResults(1) and call criteria.uniqueResult()

Upvotes: 1

Related Questions