Comments (8)
@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.
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.
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.
Have you tried using @org.hibernate.annotations.SoftDelete
instead of @SQLDelete
and @SQLRestriction
?
from spring-data-jpa.
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.
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.
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.
Yeah fair enough, thank you both! I'll close this issue.
from spring-data-jpa.
Related Issues (20)
- Spring data JPA error for field with @jakarta.persistence.Convert HOT 1
- Native query with #sort generates a broken query HOT 2
- Optimistic Locking with finding in transactions HOT 4
- Bundle Javadoc with Antora documentation site
- Exceptions in `NamedQuery` creation lead to query derivation HOT 1
- Invalid stackoverflow links on the Overview tab HOT 1
- Code Clean-Up and Refactoring Suggestions in JpaMetamodel HOT 4
- two entities with the same table name - merged table HOT 3
- Data JPA starter dependency HOT 1
- Fix CDI integration test
- Execute error after packaging using SpringNative HOT 3
- Align `AuditingEntityWithEmbeddableListenerTests.auditsEmbeddedCorrectly()` with aggregate lifecycle expectations
- Add build profile for Hibernate 6.6 compatibility
- Spring data JPA fails to sort with JSONB column HOT 3
- Upgrade to Hibernate 6.4.10.Final
- Upgrade to Hibernate 6.5.2.Final
- Upgrade to Maven Wrapper 3.9.8
- Upgrade to Maven Wrapper 3.9.8
- Upgrade to Maven Wrapper 3.9.8
- Pageable inconsistent with hateoas pagemetadata int/long
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa.