Skip to content

flush() doesn't work by default in @Transaction #3894

Closed as not planned
Closed as not planned
@chikacow

Description

@chikacow

Hi, I noticed a problem that when I am not manually using entityManager.flush() below, the result i got for tableNo in return statement and what I have in the database(which got the data updated) is different. For example, my input for tableNo is "a b c" and my expected result both for in database and the return is "abc". However I only have "abc" in the database and still "a b c" in the return statement. When I uncomment the entityManager.flush() things work perfectly, but do I have to do so since the flush() should be automatically done?

Seat.java

  @PreUpdate
    private void preUpdate() {
        this.tableNo = this.tableNo.trim().replaceAll("\\s+", "");;
    } 

SeatService.java

  @Transactional
      public Seat.SeatResponseDTO updateSeat(Long id, Seat.SeatRequestDTO requestDTO) {
          Seat seat = getSeatById(id); //which called
  
          if (!seat.getTableNo().equals(requestDTO.getTableNo())) {
              if (seatRepository.existsByTableNo(requestDTO.getTableNo())) {
                  throw new IllegalArgumentException("Table number already exists");
              }
              seat.setTableNo(requestDTO.getTableNo());
          }
  
          seat.setDescription(requestDTO.getDescription());
          seat.setStatus(TableStatus.fromString(requestDTO.getStatus()));
  
          /**
           * bug, this should be automatically called but i have to do it manually to achieve something obvious
           */
          entityManager.flush();
          
          return Seat.SeatResponseDTO.builder()
                  .tableNo(seat.getTableNo())
                  .description(seat.getDescription())
                  .status(seat.getStatus())
                  .build();
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions