I need to retrieve paginated result with fetch and I am also getting duplicates in result. It happens when I am getting entities that have more than one attribute bound by @ManyToOne relation. Here is the pseudocode:
public class Entity {
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "entity")
private Set<EntityAttribute> attributes;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, insertable = false, updatable = false)
private EntityType entityType;
}
public class EntityAttribute {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, insertable = false, updatable = false)
private Entity entity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, insertable = false, updatable = false)
private Attribute attribute;
}
@Override
public PagedList<Entity> search(SearchByCodeRequest request) {
var date = Optional.ofNullable(request.getDate()).orElse(new Date());
var pageSize = request.getPageSize();
var firstResult = request.getPage() * pageSize;
var cb = criteriaBuilderFactory.create(entityManager, Entity.class)
.fetch("entityType", "attributes", "attributes.attribute")
.orderByAsc("id")
.page(firstResult, pageSize)
.withCountQuery(true)
.where("validFrom").le(date)
.where("validTo").ge(date);
var code = request.getCode();
if (code != null) {
cb.where("entityType.code").eq(code);
}
var r = cb.getResultList();
return r;
}
As you can see I get the same entity with Id = 3 4 times in the result set, due to 4 attributes bound to that entity. How can I remove such duplicates in result set? Thank you in advance
Expected behavior
No duplicates are returned in result
Actual behavior
Duplicates are returned when entity has several @OneToMany or @ManyToMany bonds fetched
Description
I need to retrieve paginated result with fetch and I am also getting duplicates in result. It happens when I am getting entities that have more than one attribute bound by @ManyToOne relation. Here is the pseudocode:
Here is pseudo result I am getting:
As you can see I get the same entity with Id = 3 4 times in the result set, due to 4 attributes bound to that entity. How can I remove such duplicates in result set? Thank you in advance
Expected behavior
No duplicates are returned in result
Actual behavior
Duplicates are returned when entity has several @OneToMany or @ManyToMany bonds fetched
Environment
Version: 1.6.11 JPA-Provider: Hibernate 6.4.4.Final DBMS: PostgreSQL 16