mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1605 - RuntimeException: Nothing in batch? ... when lazy loading on partially populated L2 bean cache
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
@Cache
|
||||
public class PersonCacheEmail {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@ManyToOne
|
||||
private PersonCacheInfo personInfo;
|
||||
|
||||
private String email;
|
||||
|
||||
public PersonCacheEmail(String id, String email) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PersonCacheInfo getPersonInfo() {
|
||||
return personInfo;
|
||||
}
|
||||
|
||||
public void setPersonInfo(PersonCacheInfo personInfo) {
|
||||
this.personInfo = personInfo;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Cache
|
||||
public class PersonCacheInfo {
|
||||
|
||||
@Id
|
||||
private String personId;
|
||||
|
||||
private String name;
|
||||
|
||||
public PersonCacheInfo(String personId, String name) {
|
||||
this.personId = personId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
|
||||
public void setPersonId(String personId) {
|
||||
this.personId = personId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.tests.cache.personinfo;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PersonCacheTests {
|
||||
|
||||
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);
|
||||
|
||||
Ebean.save(person);
|
||||
Ebean.save(personEmail);
|
||||
}
|
||||
|
||||
private void addTestData() {
|
||||
|
||||
insert(1, "testA");
|
||||
insert(2, "testB");
|
||||
insert(3, "testC");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInQuery() {
|
||||
|
||||
addTestData();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Ebean.find(PersonCacheInfo.class)
|
||||
.select("personId") // do not fetch name
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
ServerCache beanCache = Ebean.getDefaultServer().getServerCacheManager().getBeanCache(PersonCacheInfo.class);
|
||||
beanCache.getStatistics(true);
|
||||
|
||||
List<Object> ids = Arrays.asList(new String[]{"E001", "E002", "E003"});
|
||||
|
||||
List<PersonCacheEmail> emailList =
|
||||
Ebean.find(PersonCacheEmail.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
|
||||
for (PersonCacheEmail email : emailList) {
|
||||
// get property that isn't in the cached data
|
||||
assertThat(email.getPersonInfo().getName()).isNotNull();
|
||||
System.out.println(email.getPersonInfo().getName());
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(3);
|
||||
|
||||
assertThat(beanCache.getStatistics(true).getHitCount()).isEqualTo(3);
|
||||
|
||||
System.out.println("Fetch again ...");
|
||||
|
||||
emailList =
|
||||
Ebean.find(PersonCacheEmail.class)
|
||||
.where().idIn(ids)
|
||||
.setUseCache(true)
|
||||
.findList();
|
||||
|
||||
|
||||
for (PersonCacheEmail email : emailList) {
|
||||
// get property but it is in the cache data now
|
||||
System.out.println(email.getPersonInfo().getName());
|
||||
assertThat(email.getPersonInfo().getName()).isNotNull();
|
||||
}
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
assertThat(beanCache.getStatistics(true).getHitCount()).isEqualTo(3);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user