By using the hibernate @Transactional
annotation, if you get an object from the database with lazy fetched attributes, you can simply get these by fetching these attributes like this :
@Transactional
public void checkTicketSalePresence(UUID ticketUuid, UUID saleUuid) {
Optional<Ticket> savedTicketOpt = ticketRepository.findById(ticketUuid);
savedTicketOpt.ifPresent(ticket -> {
Optional<Sale> saleOpt = ticket.getSales().stream().filter(sale -> sale.getUuid() == saleUuid).findFirst();
assertThat(saleOpt).isPresent();
});
}
Here, in an Hibernate proxy-managed transaction, the fact of calling ticket.getSales()
do another query to fetch sales because you explicitly asked it.