#2376 - When Lazy load of soft deleted bean - javax.persistence.EntityNotFoundException: Bean not found during lazy load or refresh. id[*] type[class *]

This commit is contained in:
Rob Bygrave
2021-09-16 23:01:04 +12:00
parent b262c2e19d
commit 3865fc8ffd
5 changed files with 71 additions and 16 deletions
@@ -1,6 +1,7 @@
package org.tests.cache.personinfo;
import io.ebean.annotation.Cache;
import io.ebean.annotation.SoftDelete;
import javax.persistence.Entity;
import javax.persistence.Id;
@@ -16,6 +17,9 @@ public class PersonCacheInfo {
private String name;
@SoftDelete
boolean deleted;
public PersonCacheInfo(String personId, String name) {
this.personId = personId;
this.name = name;
@@ -36,4 +40,13 @@ public class PersonCacheInfo {
public void setName(String name) {
this.name = name;
}
public boolean deleted() {
return deleted;
}
public PersonCacheInfo deleted(boolean deleted) {
this.deleted = deleted;
return this;
}
}
@@ -21,15 +21,12 @@ public class PersonCacheTests {
private final List<Object> ids = Arrays.asList("E001", "E002", "E003");
private ServerCache beanCacheInfo = DB.getDefault().cacheManager().beanCache(PersonCacheInfo.class);
private ServerCache beanCacheEmail = DB.getDefault().cacheManager().beanCache(PersonCacheEmail.class);
private ServerCacheRegion region = DB.getDefault().cacheManager().region("email");
private final ServerCache beanCacheInfo = DB.getDefault().cacheManager().beanCache(PersonCacheInfo.class);
private final ServerCache beanCacheEmail = DB.getDefault().cacheManager().beanCache(PersonCacheEmail.class);
private final ServerCacheRegion region = DB.getDefault().cacheManager().region("email");
private void insert(int id, String email) {
PersonCacheInfo person = new PersonCacheInfo("P00" + id, "P00" + id);
PersonCacheEmail personEmail = new PersonCacheEmail("E00" + id, email);
personEmail.setPersonInfo(person);
@@ -38,7 +35,6 @@ public class PersonCacheTests {
}
private void addTestData() {
insert(1, "testA");
insert(2, "testB");
insert(3, "testC");
@@ -0,0 +1,49 @@
package org.tests.cache.personinfo;
import io.ebean.DB;
import io.ebean.test.LoggedSql;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class TestCacheSoftDeleteLazyLoad {
private final String personEmailId = "SDLL-Email-01";
private final String personId = "SDLL-Person-01";
void setup(){
PersonCacheInfo person = new PersonCacheInfo(personId , "Hello");
PersonCacheEmail personEmail = new PersonCacheEmail(personEmailId, "hello@foo.com");
personEmail.setPersonInfo(person);
DB.save(person);
DB.save(personEmail);
}
@Test
void softDeleteReferencedBean_when_lazyLoad_expect() {
setup();
// load cache
DB.find(PersonCacheEmail.class, personEmailId);
// soft delete our bean referenced by email
PersonCacheInfo info = DB.find(PersonCacheInfo.class, personId);
DB.delete(info);
// load from cache (still references a now soft deleted bean)
PersonCacheEmail email2 = DB.find(PersonCacheEmail.class, personEmailId);
assert email2 != null;
LoggedSql.start();
PersonCacheInfo personInfo = email2.getPersonInfo();
assertThat(personInfo).isNotNull();
assertThat(personInfo.deleted()).isTrue(); // lazy load includes soft deletes
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0))
.contains(" from person_cache_info t0 where t0.person_id = ?")
.doesNotContain("and t0.deleted =");
}
}