In spring data jpa one can use an entitygraph to specify exactly what data needs to load.I am however stuck on when I don’t want to fetch the whole subcollection. I was wondering how I can achieve something like this. Suppose you have a translation table:
@Entity
class LocalizedString(
@Id
@GeneratedValue
val id: Long? = null,
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL], orphanRemoval = true)
var values: MutableList<LocalizedStringValue>
)
@Entity
class LocalizedStringValue(
@Id
@GeneratedValue
val id: Long? = null,
val locale: String,
val value: String
)
Is there a way is can do something like:
fetchGraph.addSubgraph(LocalizedString_.values).filter(cb.equal(LocalizedStringValue_.locale,"en"))
So fetch only a single local
I tried various solutions:
Hibernate filters, this didn’t seem to work for both lazy and eagerly fetching the collection.
Putting where clauses and joins in queries. The problem here is that this modifies the data the query. Whereas it should only be about the retrieved data.
The only thing that worked was putting @SQlRestriction on the collection. But this does not allow for dynamic properties.