Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE] CrudRepository save() Method issue #18285

Open
YinMinghhh opened this issue Feb 10, 2025 · 0 comments
Open

[ISSUE] CrudRepository save() Method issue #18285

YinMinghhh opened this issue Feb 10, 2025 · 0 comments
Labels
on-jira triaged Issues reviewed by a dev and considered valid. Will be added in Jira.

Comments

@YinMinghhh
Copy link

Hello, there may be a bug.

This article reads "So, any change to that entity will be automatically persisted in the database, regardless of whether we are invoking the save() method." and gave such an example:

@Test
@Transactional
public void shouldUpdateExistingEntryInDBWithoutSave() {
    MerchandiseEntity pants = new MerchandiseEntity(
      ORIGINAL_TITLE, BigDecimal.ONE);
    pants = repository.save(pants);

    Long originalId = pants.getId();

    // Update using setters
    pants.setTitle(UPDATED_TITLE);
    pants.setPrice(BigDecimal.TEN);
    pants.setBrand(UPDATED_BRAND);

    Optional<MerchandiseEntity> resultOp = repository.findById(originalId);

    assertTrue(resultOp.isPresent());
    MerchandiseEntity result = resultOp.get();

    assertEquals(originalId, result.getId());
    assertEquals(UPDATED_TITLE, result.getTitle());
    assertEquals(BigDecimal.TEN, result.getPrice());
    assertEquals(UPDATED_BRAND, result.getBrand());
}

I found that the test did pass, but findById() simply reads data from the cache, and the data in the database is not updated because save() is not called.

If the cache is cleared between save() and findById() (e.g. by calling entityManager.clear()), the test cannot pass.

So the statement in the article is not quite correct.

@ulisseslima ulisseslima added the triaged Issues reviewed by a dev and considered valid. Will be added in Jira. label Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on-jira triaged Issues reviewed by a dev and considered valid. Will be added in Jira.
Projects
None yet
Development

No branches or pull requests

3 participants