'.map() does not return the updated persisted object

I'm trying to create a new database entry with a newly created TicketToken object. I then want to update the Ticket object with the generated id on a foreign key field. I create the TicketToken object fine and it appears in the database with the generated value but during map, the object has null fields.

Uni<Long> tokenUni = ticketService.findTicketById(ticketId).map(Ticket::getTicketTokenId);

    Uni<Long> newTokenId = tokenUni.onItem().ifNull().switchTo(Uni.createFrom().item(new TicketTokenId())
            .chain(ticketTokenId -> tokenIdService.persistTicketTokenId(ticketTokenId))
            .map(createdToken -> {
                log.info(createdToken.toString());
                ticketService.findTicketById(ticketId).onItem().invoke(ticket -> {
                    ticket.setTicketTokenId(createdToken.getTokenId());
                    ticketService.updateTicket(ticket).subscribe().with(item -> log.info("Successfully updated ticket -> " + item.toString()));
                }).subscribe().with(item -> log.info("Updated ticket with tokenId"));
                return createdToken.tokenId;
            })
    );

That first log should have a TicketTokenId object with like {id: 1, field2: null, field3: null, ...} but it has a null object {id: null, field2: null, field3: null, ...} which means it does not return the actual persisted object. How can I get the id so I can link it after in the other object?



Solution 1:[1]

I fixed it. The reason I did not have the persisted object updated with the id is because the persistTicketTokenId method used the Panache persist method only. I had to flush it after using the method persistAndFlush

public Uni<TicketTokenId> persistTicketTokenId(@Valid TicketTokenId ticketTokenId) {
    // For now, ticket creation is restricted to admin accounts. Later it will be implemented for venue users
    if (this.currentUser.getRole() != Roles.ADMIN) {
        log.warn("User does not have permissions to create a ticket");
        return Uni.createFrom().failure(new ForbiddenException());
    }
    return Panache.withTransaction(ticketTokenId::persistAndFlush);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Francisco Ribeiro