quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.83k stars 2.7k forks source link

How do I make the Entities to not get detached when using nested Unis #44635

Closed underscore05 closed 2 hours ago

underscore05 commented 6 hours ago

Discussed in https://github.com/quarkusio/quarkus/discussions/44621

Originally posted by **underscore05** November 22, 2024 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 `ava.util.concurrent.CompletionException: org.hibernate.PersistentObjectException: detached entity passed to persist`. I suspect that the agent and customer is already detached at that point but I don't know what to do to attached them again. I am using reactive-panache and repository for agent, customer and jobposting entity. ```java 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); }); ```
geoand commented 2 hours ago

Let's keep the discussion