hibernate - How do I specify DISTINCT when using JPA 2? -
i'm using hibernate 4.1.0.final, mysql 5.5, , jpa 2.0. when returning values jpa query, how specify "distinct" clause without rewriting function sql (or jpql) statement? below if how have coded jpa method far ...
final list<returnedobject> foundreturnedobjects = new arraylist<returnedobject>(); final criteriabuilder builder = m_entitymanager.getcriteriabuilder(); final criteriaquery<returnedobject> query = builder.createquery(returnedobject.class); final root<joinedobject> joinedobject = query.from(joinedobject.class); final join<joinedobject, returnedobject> returnedobject = joinedobject.join(joinedobject_.returnedobject); if (params != null && !params.isempty()) { query.where(joinedobject.get(joinedobject_.organization).in(params)); query.select(returnedobject); foundreturnedobjects.addall(m_entitymanager.createquery(query).getresultlist()); } // if return foundreturnedobjects;
however, problem duplicate items potentially returned. note, realize transfer results java.util.set, question executing sql distinct clause instead of doing work after query has returned.
that can done using distinct method, can found criteriaquery itself:
query.distinct(true);
Comments
Post a Comment