0

I'm working on updating a project from Spring Boot 2.7 to 3.1, and one unit test for a repository is throwing me for a loop. The entity is

@Entity
@Data
@NoArgsConstructor
@Table(name = "RETRY", schema = "DOCUMENT")
public class RetryEntity {
    public RetryEntity(UUID retryId) {
        this.retryId = retryId;
    }

    @Id
    @Column(name = "RETRY_UID", length = 36, nullable = false)
    private UUID retryId;

    @Column(name = "LATEST_CORRESPONDENCE_UID", length = 36, nullable = false)
    private UUID latestCorrespondenceId;

    @Column(name = "LATEST_FLOW_NAME_DC", nullable = false)
    private String latestFlowName;

    @Column(name = "LATEST_RETRY_TS", nullable = false)
    private Timestamp latestRetryTs;

    @Enumerated(EnumType.STRING)
    @Column(name = "STATUS_CD", nullable = false)
    private RetryStatusCode statusCode;

    @Column(name = "RETRY_CNT", nullable = false)
    private Integer retryCount;

    @CreationTimestamp
    @Column(name = "RECORD_CREATE_GMTS", nullable = false)
    private Timestamp recordCreateGmts;

    @UpdateTimestamp
    @Column(name = "RECORD_UPDATE_GMTS")
    private Timestamp recordUpdateGmts;
}

The repository has one method:

public interface RetryRepository extends JpaRepository<RetryEntity, UUID> {
    @Query("select r from RetryEntity r where r.statusCode='PENDING'")
    List<RetryEntity> findByStatusCodePending();
}

The test looks like this:

@DataJpaTest
public class RetryRepositoryTest {
    @Autowired private RetryRepository retryRepository;

    @Autowired private TestEntityManager entityManager;

    @Test
    public void findByStatusCodePending_shouldOnlyGetPending() {
        UUID CORRESPONDENCE_ID = randomUUID();
        String FLOW_NAME = "FOOBAR";
        Integer retryCount = 1;
        Timestamp latestRetryTs = new Timestamp(12345L);

        RetryEntity retryEntity1 = new RetryEntity(randomUUID());
        retryEntity1.setLatestCorrespondenceId(CORRESPONDENCE_ID);
        retryEntity1.setLatestFlowName(FLOW_NAME);
        retryEntity1.setRetryCount(retryCount);
        retryEntity1.setStatusCode(RetryStatusCode.FAILED);
        retryEntity1.setLatestRetryTs(latestRetryTs);

        RetryEntity retryEntity2 = new RetryEntity(randomUUID());
        retryEntity2.setLatestCorrespondenceId(CORRESPONDENCE_ID);
        retryEntity2.setLatestFlowName(FLOW_NAME);
        retryEntity2.setRetryCount(retryCount);
        retryEntity2.setStatusCode(RetryStatusCode.PENDING);
        retryEntity2.setLatestRetryTs(latestRetryTs);

        RetryEntity retryEntity3 = new RetryEntity(randomUUID());
        retryEntity3.setLatestCorrespondenceId(CORRESPONDENCE_ID);
        retryEntity3.setLatestFlowName(FLOW_NAME);
        retryEntity3.setRetryCount(retryCount);
        retryEntity3.setStatusCode(RetryStatusCode.COMPLETE);
        retryEntity3.setLatestRetryTs(latestRetryTs);

        List<RetryEntity> saved =
                retryRepository.saveAll(List.of(retryEntity1, retryEntity2, retryEntity3));

        entityManager.flush();
        List<RetryEntity> found = retryRepository.findAll();

The problem is that when I inspect the entities in the found list, the values in both UUID columns (retryId and latestCorrespondenceId) have changed compared to what they are in the saved list. I have tried to dig into the JPA and Hibernate code to figure out where they're changing, but since it's not on save, I'm baffled. Why are these values changing?

spartanhooah
  • 183
  • 4
  • 15

0 Answers0