How do I make the Entities to not get detached when using nested Unis #44621
-
I have this code where I try to get an entity in a sequential manner. At the last flatMap, I can see that agent and customer is still assigned to newJobPosting. But persisting it causes an error like Log.info("Creating new job-posting");
return Uni.createFrom().item(() -> modelMapper.map(request, JobPosting.class))
.flatMap((newJobPosting) -> getAgentOrException(jwtId).map(agent -> {
newJobPosting.setAgent(agent);
return newJobPosting;
}))
.flatMap((newJobPosting) -> {
if(request.getAssignedCustomerId() != null && JobPostingVisibility.PRIVATE_POST.equals(request.getVisibility())) {
return getCustomerOrException(request.getAssignedCustomerId())
.map(customer -> {
newJobPosting.setCustomer(customer);
return newJobPosting;
});
}
return Uni.createFrom().item(newJobPosting);
})
.flatMap(newJobPosting -> {
if(newJobPosting.getAgent() != null) {
Log.infof("Has Agent! %s", newJobPosting.getAgent().getJwtId());
}
if(newJobPosting.getCustomer() != null) {
Log.infof("Has Customer! %s", newJobPosting.getCustomer().getJwtId());
}
return jobPostRepository.persist(newJobPosting);
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
cc @DavideD |
Beta Was this translation helpful? Give feedback.
-
Without looking at the rest of the code and the mapping, I'm not sure what's the best solution. To reattach an object to the session:
Reloading it works if the object doesn't have any changes you want to save. the Hibernate session has several methods to do it. Otherwise, you can merge it using I don't know why, but Panache doesn't seem to have a merge operator out of the box, but adding this to your repository should work: class JobPostRepository implements PanacheRepository<JobPost> {
// ...
public Uni<JobPost> merge(JobPost jobPost) {
return Panache.getSession().chain( session -> session.merge( jobPost ) );
}
} You should be able to adapt this example to what you need. I hope this help. |
Beta Was this translation helpful? Give feedback.
Hello @DavideD closing this discussion.
Chaining the
flatMap
does not make the entity to get detached.It's a different issue all along.
Changing
modelMapper.map(request, JobPosting.class)
to justnew JobPosting()
fixes my code. Thanks for your time.