JPA specification equivalent to ResultTransformer.DISTINCT_ROOT_ENTITY?

I was wondering if there is an equivalent ResultTransformer.DISTINCT_ROOT_ENTITY for use in JPA specifications? I'm trying to implement a search function right now, and for this I have to use the JPA specifications (I am not allowed to use CriteriaQuery or other alternatives).

I need to execute JOIN in a search query because there are one, many relationships between objects.

So right now I am doing this:

String type = "admin";
SpecificationBuilder sb = SpecificationBuilder.getInstance();
Specification<WebContentImpl> spec = new Specification<WebContentImpl>() {
    @Override
    public Predicate toPredicate(Root<WebContentImpl> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
        return cb.equal(root.join("users", JoinType.LEFT).get("type"), type);
    }
};
sb.addSpecification(spec);

As you can see, I can enable JOIN. Multiple users are mapped to a single web content item. However, when, for example, 3 users are mapped to 1 web content item, the web content item should be displayed only 1 time, but it is shown 3 times. I had this problem in the past, and then the ResultTransformer.DISTINCT_ROOT_ENTITY flag appeared, but I can not use this function now.

Of course, I can implement my own solution to eliminate doubles, but it will be terrible for performance. And I already tried changing the JoinType, but the result was always the same.

Is there anyone with some tips to help me ?:-)

Thanks in advance!

Regards, K

+4
source share
2

group by :

cq.groupBy(root.get("id"));

, WebContentImpl.is .

0

Source: https://habr.com/ru/post/1539738/


All Articles