mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
git push origin masterMerge branch 'Ryszard-Trojnacki-master'
This commit is contained in:
@@ -169,7 +169,7 @@ public class LoadManyRequest extends LoadRequest {
|
||||
Object parentId = desc.getId(ownerBean);
|
||||
logger.debug("BeanCollection after lazy load was empty. type:" + ownerBean.getClass().getName() + " id:" + parentId + " owner:" + ownerBean);
|
||||
}
|
||||
} else if (isLoadCache()) {
|
||||
} else if (isLoadCache() && many.isUseCache()) {
|
||||
Object parentId = desc.getId(bc.getOwnerBean());
|
||||
desc.cacheManyPropPut(many, bc, parentId);
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ public class CacheChangeSet {
|
||||
|
||||
final ManyKey key;
|
||||
|
||||
final List<Object> removes = new ArrayList<>();
|
||||
final Set<Object> removes = new HashSet<>();
|
||||
|
||||
final Map<Object, CachedManyIds> puts = new LinkedHashMap<>();
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ class DefaultBeanLoader {
|
||||
parentDesc.contextPut(pc, parentId, parentBean);
|
||||
}
|
||||
|
||||
boolean useManyIdCache = beanCollection != null && parentDesc.isManyPropCaching();
|
||||
boolean useManyIdCache = beanCollection != null && parentDesc.isManyPropCaching() && many.isUseCache();
|
||||
if (useManyIdCache) {
|
||||
Boolean readOnly = null;
|
||||
if (ebi.isReadOnly()) {
|
||||
|
||||
@@ -226,6 +226,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
|
||||
desc.registerTable(targetDescriptor.getBaseTable(), this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the underlying collection of beans.
|
||||
*/
|
||||
@@ -345,7 +346,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
|
||||
|
||||
/**
|
||||
* Add the loaded current bean to its associated parent.
|
||||
*
|
||||
* <p>
|
||||
* Helper method used by Elastic integration when loading with a persistence context.
|
||||
*/
|
||||
@Override
|
||||
@@ -967,6 +968,13 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
|
||||
elementDescriptor.jsonWriteElement(ctx, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only cache Many Ids if the target bean is also cached.
|
||||
*/
|
||||
public boolean isUseCache() {
|
||||
return targetDescriptor.isBeanCaching();
|
||||
}
|
||||
|
||||
/**
|
||||
* A Many (element collection) property in bean cache to held as JSON.
|
||||
*/
|
||||
|
||||
@@ -209,7 +209,7 @@ class DLoadManyContext extends DLoadBaseContext implements LoadManyContext {
|
||||
public void loadMany(BeanCollection<?> bc, boolean onlyIds) {
|
||||
|
||||
synchronized (this) {
|
||||
boolean useCache = context.hitCache && !onlyIds;
|
||||
boolean useCache = !onlyIds && context.hitCache && context.property.isUseCache();
|
||||
if (useCache) {
|
||||
EntityBean ownerBean = bc.getOwnerBean();
|
||||
BeanDescriptor<?> parentDesc = context.desc.getBeanDescriptor(ownerBean.getClass());
|
||||
|
||||
+42
-2
@@ -141,11 +141,11 @@ public class TestCacheCollectionIds extends BaseTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangingCollectionByEditingConnectedBean() {
|
||||
public void testChangingCollectionByEditingConnectedCachedBean() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello");
|
||||
cachedBean.setName("hello1");
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
awaitL2Cache();
|
||||
@@ -180,6 +180,46 @@ public class TestCacheCollectionIds extends BaseTestCase {
|
||||
Ebean.delete(cachedBean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangingCollectionByEditingConnectedNotCachedBean() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
OCachedBean cachedBean = new OCachedBean();
|
||||
cachedBean.setName("hello2");
|
||||
|
||||
Ebean.save(cachedBean);
|
||||
awaitL2Cache();
|
||||
|
||||
OCachedBean dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
OBeanChild child1=new OBeanChild();
|
||||
child1.setCachedBean(cachedBean);
|
||||
Ebean.insert(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
OBeanChild child2=new OBeanChild();
|
||||
child2.setCachedBean(cachedBean);
|
||||
Ebean.insert(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(2, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
Ebean.delete(child2);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(1, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
Ebean.delete(child1);
|
||||
awaitL2Cache();
|
||||
dummyToLoad = Ebean.find(OCachedBean.class, cachedBean.getId());
|
||||
assertEquals(0, dummyToLoad.getNotCachedChildren().size());
|
||||
|
||||
Ebean.delete(cachedBean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* When updating a ManyToMany relations also the collection cache must be updated.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.tests.model.basic;
|
||||
|
||||
import io.ebean.annotation.Cache;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* Not cached bean (copy of {@link OCachedBeanChild}} without {@link Cache} annotation
|
||||
* for testing caching implementation for relations {@link Cache} and not {@link Cache}.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "o_bean_child")
|
||||
public class OBeanChild {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@ManyToOne
|
||||
OCachedBean cachedBean;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public OCachedBean getCachedBean() {
|
||||
return cachedBean;
|
||||
}
|
||||
|
||||
public void setCachedBean(OCachedBean cachedBean) {
|
||||
this.cachedBean = cachedBean;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,9 @@ public class OCachedBean {
|
||||
@OneToMany(mappedBy = "cachedBean", cascade = CascadeType.ALL)
|
||||
List<OCachedBeanChild> children = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "cachedBean", cascade = CascadeType.ALL)
|
||||
List<OBeanChild> notCachedChildren = new ArrayList<>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -61,4 +64,12 @@ public class OCachedBean {
|
||||
public void setChildren(List<OCachedBeanChild> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public List<OBeanChild> getNotCachedChildren() {
|
||||
return notCachedChildren;
|
||||
}
|
||||
|
||||
public void setNotCachedChildren(List<OBeanChild> notCachedChildren) {
|
||||
this.notCachedChildren = notCachedChildren;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user