Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: correct caching when collection with different inherited beans is put to the cache #1379

Merged
merged 2 commits into from May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -447,7 +447,21 @@ CachedBeanData beanExtractData(BeanDescriptor<?> targetDesc, EntityBean bean) {
void beanPutAll(Collection<EntityBean> beans) {
if (desc.inheritInfo != null) {
Class<?> aClass = theClassOf(beans);
desc.descOf(aClass).cacheBeanPutAllDirect(beans);
// check if all beans have the same class
for (EntityBean bean : beans) {
if (!bean.getClass().equals(aClass)) {
aClass = null;
break;
}
}
if (aClass == null) {
// there are different bean types in the collection, so we add one by one to the cache
for (EntityBean bean : beans) {
desc.descOf(bean.getClass()).cacheBeanPutDirect(bean);
}
} else {
desc.descOf(aClass).cacheBeanPutAllDirect(beans);
}
} else {
beanCachePutAllDirect(beans);
}
Expand Down
Expand Up @@ -4,10 +4,14 @@
import io.ebean.Ebean;
import org.tests.model.basic.cache.CInhOne;
import org.tests.model.basic.cache.CInhRef;
import org.tests.model.basic.cache.CInhRoot;
import org.tests.model.basic.cache.CInhTwo;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.ebeantest.LoggedSqlCollector;
Expand Down Expand Up @@ -61,4 +65,47 @@ public void test() {
assertThat(sql).hasSize(0);

}

@Test
public void testMap() {

List<Integer> ids = new ArrayList<>();

CInhOne one = new CInhOne();
one.setLicenseNumber("O19");
one.setDriver("Foo");
one.setNotes("Hello");
Ebean.save(one);

ids.add(one.getId());

CInhTwo two = new CInhTwo();
two.setLicenseNumber("T23");
two.setAction("Test");
Ebean.save(two);

ids.add(two.getId());


LoggedSqlCollector.start();

Ebean.find(CInhRoot.class).setUseQueryCache(true).where().idIn(ids).setMapKey("licenseNumber").findMap();

List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("from cinh_root");

// try some cache finds
Ebean.find(CInhRoot.class).setUseQueryCache(true).findList();
Ebean.find(CInhRoot.class).setUseQueryCache(true).findList();
Ebean.find(CInhRoot.class).setUseQueryCache(true).findList();

LoggedSqlCollector.start();

Ebean.find(CInhRoot.class).setUseQueryCache(true).where().idIn(ids).setMapKey("licenseNumber").findMap();

sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(0);
}

}
2 changes: 1 addition & 1 deletion src/test/java/org/tests/model/basic/cache/CInhRoot.java
Expand Up @@ -7,7 +7,7 @@
import javax.persistence.Entity;
import javax.persistence.Inheritance;

@Cache
@Cache(enableQueryCache = true)
@Entity
@Inheritance
@DiscriminatorColumn(length = 3)
Expand Down