If I have the following mapped collection in a POJO using JPA/Hibernate Annotations:
@OneToMany(mappedBy = "columnName", fetch = FetchType.LAZY)
@MapKeyJoinColumn(name = "id_fk")
@LazyCollection(value = LazyCollectionOption.EXTRA)
private Set<PojoClass> collection = new HashSet<>();
That's not possible. Hibernate has two options for you:
There's no middle ground. That's because Hibernate needs to manage the whole collection entity state transitions. If you were able to load subsets then a unidirectional bag wouldn't make much sense.
While you can use
@Filter, these annotations are more useful for filtering out the content of the collection, not to restrict its size.
So, you must always remember that Hibernate collections are a feature, not a mandatory requirement. Queries are way more flexible and less limiting.
So, you have to use queries this time: