Hibernate reactive panache pagination with OneToMany entity #38643
-
We're using hibernate-reactive-panache v3.7.1 with these two entities @Entity
@Table(name = "owner")
@NamedQuery(name = "Owner.findById", query = "from Owner o left join fetch o._pets p where o.id = :id")
class Owner() {
@OneToMany(
cascade = [CascadeType.ALL],
fetch = FetchType.LAZY,
mappedBy = "owner",
orphanRemoval = true
)
private val _pets = mutableListOf<Pet>()
val pets get() = _pets.toList()
fun addPet(pet: Pet) {
if (_pets.contains(pet)) {
throw IllegalArgumentException("The same pet can not be assigned to the same owner again!")
}
this._pets.add(pet)
}
}
@Entity
@Table(name = "pet")
class Pet() {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "owner_id")
@JsonIgnore // ignore to prevent infinite recursion when serialization
lateinit var owner: Owner
} and we want to use paging as described on the guide here. This stackowerflow question suggests to use a So we tried to add a second @NamedQuery(name = "Owner.countById", query = "from Owner o where o.id = :id") to mimic the val params = Parameters.with("id", id)
ownerRepository
.find("#Owner.findById", params).page<Owner>(pageIndex, pageSize).list<Owner>()
.flatMap { owners ->
val query = repo.find("#Owner.countById", params).page<Owner>(pageIndex, pageSize)
Uni.combine().all().unis(
Uni.createFrom().item(query.hasPreviousPage()),
query.hasNextPage(),
query.pageCount(),
query.count()
).with { hasPrev, hasNext, pageCount, count ->
OwnerPage(result = owners, hasPrev = hasPrev, hasNext = hasNext, pageCount = pageCount, count = count)
}
} but this throws the error Do you have any suggestions on how this can be implemented? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
/cc @DavideD (hibernate-reactive), @FroMage (panache), @gavinking (hibernate-reactive), @geoand (kotlin), @loicmathieu (panache) |
Beta Was this translation helpful? Give feedback.
-
The problem is that, at the moment, operations using the session must run in a precise order. Basically, this means that you cannot use |
Beta Was this translation helpful? Give feedback.
The problem is that, at the moment, operations using the session must run in a precise order. Basically, this means that you cannot use
Uni.combine().all().unis(...)
.It should work if you compose/chain the operations.