From ee393c6485fc5376172be71649bf3a03fdebda8a Mon Sep 17 00:00:00 2001 From: Noemi Szemenyei Date: Tue, 19 Jan 2021 11:41:19 +0100 Subject: [PATCH 1/3] ADD: testcase for wrong lazyload with cache --- .../model/lazywithcache/ChildWithCache.java | 52 ++++++++++++++++++ .../tests/model/lazywithcache/ParentA.java | 46 ++++++++++++++++ .../tests/model/lazywithcache/ParentB.java | 36 +++++++++++++ .../TestWithCacheAndLazyLoad.java | 54 +++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 ebean-core/src/test/java/org/tests/model/lazywithcache/ChildWithCache.java create mode 100644 ebean-core/src/test/java/org/tests/model/lazywithcache/ParentA.java create mode 100644 ebean-core/src/test/java/org/tests/model/lazywithcache/ParentB.java create mode 100644 ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java 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..e245fea79 --- /dev/null +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java @@ -0,0 +1,54 @@ +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 + + String tempLazyProp = temp.getAddress(); + assertThat(tempLazyProp).isEqualTo("Address"); + + } + +} From 9dd28d88796331dbdcb0abb8515dafcd7e92d122 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Wed, 20 Jan 2021 21:10:49 +1300 Subject: [PATCH 2/3] #2146 - Fix for Cache with lazy load doesnt work When the list is empty() it has already been loaded and loading should skip the l2 cache --- .../server/loadcontext/DLoadBeanContext.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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)) { From f28f5c9a019d9e075194b70f3df172931e5d6fcf Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Wed, 20 Jan 2021 21:37:29 +1300 Subject: [PATCH 3/3] #2146 - no change - Tidy test only TestWithCacheAndLazyLoad --- .../TestWithCacheAndLazyLoad.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) 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 index e245fea79..ce5d35b26 100644 --- a/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java +++ b/ebean-core/src/test/java/org/tests/model/lazywithcache/TestWithCacheAndLazyLoad.java @@ -13,42 +13,40 @@ import static org.assertj.core.api.Assertions.assertThat; * @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 - + 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 - - String tempLazyProp = temp.getAddress(); - assertThat(tempLazyProp).isEqualTo("Address"); - + // 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"); } }