diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadBeanContext.java b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadBeanContext.java index 6c796501f..454fd6dac 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadBeanContext.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/loadcontext/DLoadBeanContext.java @@ -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 hits = context.desc.cacheBeanLoadAll(list, persistenceContext, ebi.getLazyLoadPropertyIndex(), ebi.getLazyLoadProperty()); list.removeAll(hits); if (list.isEmpty() || hits.contains(ebi)) { diff --git a/ebean-core/src/test/java/org/tests/model/lazywithcache/ChildWithCache.java b/ebean-core/src/test/java/org/tests/model/lazywithcache/ChildWithCache.java new file mode 100644 index 000000000..d15c36d49 --- /dev/null +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/ChildWithCache.java @@ -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; + } + +} diff --git a/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentA.java b/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentA.java new file mode 100644 index 000000000..6a096113b --- /dev/null +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentA.java @@ -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; + } + +} diff --git a/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentB.java b/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentB.java new file mode 100644 index 000000000..521d3d52b --- /dev/null +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/ParentB.java @@ -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; + } + +} diff --git a/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java b/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java new file mode 100644 index 000000000..ce5d35b26 --- /dev/null +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java @@ -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"); + } + +}