Giter Site home page Giter Site logo

Comments (8)

quaff avatar quaff commented on July 3, 2024 1

@khaledbaghdadii Your test should pass if test method annotated with @Transactional(propagation = Propagation.NOT_SUPPORTED) or call entityManager.flush();entityManager.clear(); after save, without that the entity is in persistence context and not flush to database then findById will return the managed entity not querying database and applying @SQLRestriction.

from spring-data-jpa.

schauder avatar schauder commented on July 3, 2024 1

This is just a classical problem with JPA (not Spring Data JPA). It relies heavily on the cache und doesn't reload reload data as @quaff wrote in his comment. Of course Spring Data could apply some clear or flush magic, but that comes at the very least with a performance penalty and in many cases with surprising behaviour in other places.

Therefore there isn't much we can do about this.

from spring-data-jpa.

khaledbaghdadii avatar khaledbaghdadii commented on July 3, 2024

This is a detailed of all the tests created and all of them pass as expected except for the findById when transactions are enabled

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@ContextConfiguration(initializers = SingletonPostgresDbContainerContextInitializer.class)
class TestItem {
    @Autowired
    private ItemJpaRepository repository;

    @Test
    //This test passes
    void testFindAllById(){
        ItemEntity itemEntity = ItemEntity.builder()
                .count(1)
                .deleted(true)
                .build();
        repository.save(itemEntity);
        List<ItemEntity> itemEntities = repository.findAllById(List.of(itemEntity.getId()));
        Assertions.assertThat(itemEntities).isEmpty();
    }

    @Test
        //This test fails
    void testFindById(){
        ItemEntity itemEntity = ItemEntity.builder()
                .count(1)
                .deleted(true)
                .build();
        repository.save(itemEntity);
        Optional<ItemEntity> itemEntityOptional = repository.findById(itemEntity.getId());
        Assertions.assertThat(itemEntityOptional).isEmpty();
    }

    @Test
    @Transactional(propagation = Propagation.NEVER)
        //This test passes
    void testFindByIdNonTransactional(){
        ItemEntity itemEntity = ItemEntity.builder()
                .count(1)
                .deleted(true)
                .build();
        repository.save(itemEntity);
        Optional<ItemEntity> itemEntityOptional = repository.findById(itemEntity.getId());
        Assertions.assertThat(itemEntityOptional).isEmpty();
    }

    @Test
        //This test passes
    void existsById(){
        ItemEntity itemEntity = ItemEntity.builder()
                .count(1)
                .deleted(true)
                .build();
        repository.save(itemEntity);

        Assertions.assertThat(repository.existsById(itemEntity.getId())).isFalse();
    }

    @Test
        //This test passes
    void findByIdAndCountLessThan(){
        ItemEntity itemEntity = ItemEntity.builder()
                .count(1)
                .deleted(true)
                .build();
        repository.save(itemEntity);
        repository.deleteById(itemEntity.getId());

        Optional<ItemEntity> itemEntityOptional = repository.findByIdAndCountLessThan(itemEntity.getId(),999);
        Assertions.assertThat(itemEntityOptional).isEmpty();

    }
}

from spring-data-jpa.

quaff avatar quaff commented on July 3, 2024

Have you tried using @org.hibernate.annotations.SoftDelete instead of @SQLDelete and @SQLRestriction?

from spring-data-jpa.

khaledbaghdadii avatar khaledbaghdadii commented on July 3, 2024

This works but there are some cases where I'm using the @SQLRetriction for reasons other than soft delete, and also cases where I need the @SQLDelete(sql = "UPDATE item_entity SET deleted = true WHERE id = ?") without the @SQLRestriction to be able to fetch entities that are soft deleted if they are part of another entity that is not deleted.

I'm gonna rename the issue to match the problem with @SQLRestriction instead of specifically for soft delete cause I don't think this is the correct behaviour anyways if all other JPARepository methods are working fine except for it

from spring-data-jpa.

khaledbaghdadii avatar khaledbaghdadii commented on July 3, 2024

I was able to make the test pass but I do belive there's a bug with the behaviour of findById since it is not cosistent with the other JPA methods

from spring-data-jpa.

quaff avatar quaff commented on July 3, 2024

I was able to make the test pass but I do belive there's a bug with the behaviour of findById since it is not cosistent with the other JPA methods

findById is simply delegate to entityManager.find(domainType, id) which will find managed entity from persistence context first, if you insist on it's a bug, you should open an issue for Hibernate.

from spring-data-jpa.

khaledbaghdadii avatar khaledbaghdadii commented on July 3, 2024

Yeah fair enough, thank you both! I'll close this issue.

from spring-data-jpa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.