Merge branch 'FOCONIS-cache-with-lazyload-wrong-12'

This commit is contained in:
rob bygrave
2021-01-20 21:37:57 +13:00
5 changed files with 190 additions and 4 deletions
@@ -56,7 +56,6 @@ class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext {
}
private void configureQuery(SpiQuery<?> query, String lazyLoadProperty) {
if (cache) {
query.setBeanCacheMode(CacheMode.ON);
}
@@ -70,7 +69,6 @@ class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext {
}
protected void register(EntityBeanIntercept ebi) {
if (currentBuffer.isFull()) {
currentBuffer = createBuffer(secondaryBatchSize);
}
@@ -198,8 +196,10 @@ class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext {
// lazy load property was a Many
return;
}
if (context.hitCache) {
if (list.isEmpty()) {
// re-add to the batch and lazy load from DB skipping l2 cache
list.add(ebi);
} else if (context.hitCache) {
Set<EntityBeanIntercept> hits = context.desc.cacheBeanLoadAll(list, persistenceContext, ebi.getLazyLoadPropertyIndex(), ebi.getLazyLoadProperty());
list.removeAll(hits);
if (list.isEmpty() || hits.contains(ebi)) {
@@ -0,0 +1,52 @@
package org.tests.model.lazywithcache;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import io.ebean.annotation.Cache;
/**
* Class with @Cache and lazy load property.
*
* @author Noemi Szemenyei, FOCONIS AG
*
*/
@Entity
@Cache(enableQueryCache = true)
public class ChildWithCache {
@Id
Long id;
String name;
@Basic(fetch = FetchType.LAZY)
String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
@@ -0,0 +1,46 @@
package org.tests.model.lazywithcache;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Parent class with ChildWithCache.
*
*/
@Entity
public class ParentA {
@Id
Long id;
@ManyToOne(optional = true)
ChildWithCache child;
String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public ChildWithCache getChild() {
return child;
}
public void setChild(ChildWithCache child) {
this.child = child;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,36 @@
package org.tests.model.lazywithcache;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Parent class with ChildWithCache.
*
*/
@Entity
public class ParentB {
@Id
Long id;
@ManyToOne(optional = true)
ChildWithCache child;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public ChildWithCache getChild() {
return child;
}
public void setChild(ChildWithCache child) {
this.child = child;
}
}
@@ -0,0 +1,52 @@
package org.tests.model.lazywithcache;
import org.junit.Test;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test with bean cache and lazy loaded property.
*
* @author Noemi Szemenyei, FOCONIS AG
*
*/
public class TestWithCacheAndLazyLoad extends BaseTestCase{
@Test
public void testGetters() {
ChildWithCache child = new ChildWithCache();
child.setId(1L);
child.setName("Child With Cache");
child.setAddress("Address");
DB.save(child);
ParentA parentA = new ParentA();
parentA.setId(1L);
parentA.setName("Parent A");
parentA.setChild(child);
DB.save(parentA);
ParentB parentB = new ParentB();
parentB.setId(1L);
parentB.setChild(child);
DB.save(parentB);
ParentA tempA = DB.find(ParentA.class, 1L);
tempA.getChild().getName(); // load name
ParentB tempB = DB.find(ParentB.class, 1L);
ChildWithCache temp = tempB.getChild();
// if the next line is commented out, the test passes
temp.getName(); // load name from cache --> ebean_intercept.loadedFromCache = true
assertThat(temp.getAddress()).isEqualTo("Address");
assertThat(temp.getName()).isEqualTo("Child With Cache");
}
}