Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private void loadManyInternal(EntityBean parentBean, String propertyName, boolea
if (ebi.isReadOnly()) {
query.setReadOnly(true);
}
if (many.hasOrderColumn()) {
query.orderBy(many.path() + "." + many.fetchOrderBy());
}

server.findOne(query);
if (beanCollection != null) {
Expand Down
47 changes: 47 additions & 0 deletions ebean-test/src/test/java/org/tests/cascade/TestOrderedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,51 @@ public void testModifyListWithCache() {
assertThat(masterDb.getDetails()).containsExactlyInAnyOrder(detail3, detail1);

}

@Test
public void testModifyListWithCache2() {
final OmCacheOrderedMaster master = new OmCacheOrderedMaster("Master");
final OmCacheOrderedDetail detail1 = new OmCacheOrderedDetail("Detail1");
final OmCacheOrderedDetail detail2 = new OmCacheOrderedDetail("Detail2");
final OmCacheOrderedDetail detail3 = new OmCacheOrderedDetail("Detail3");
DB.save(detail1);
DB.save(detail2);
DB.save(detail3);
master.getDetails().add(detail1);
master.getDetails().add(detail2);
master.getDetails().add(detail3);

DB.save(master);

OmCacheOrderedMaster masterDb = DB.find(OmCacheOrderedMaster.class, master.getId()); // load cache
assertThat(masterDb.getDetails()).containsExactly(detail1, detail2, detail3);

masterDb = DB.find(OmCacheOrderedMaster.class, master.getId());
assertThat(masterDb.getDetails()).containsExactly(detail1, detail2, detail3); // hit cache

// 1 und 2 tauschen
masterDb.getDetails().add(0, masterDb.getDetails().remove(1));
DB.save(masterDb);

masterDb.getDetails().remove(2);
DB.save(masterDb);

LoggedSql.start();
OmCacheOrderedMaster masterDbNew = DB.find(OmCacheOrderedMaster.class, master.getId());
masterDbNew.getDetails().size();
assertThat(masterDbNew.getDetails()).containsExactly(detail2, detail1);
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1).first().asString()
.startsWith("select t0.id, t1.id, t1.name, t1.version, t1.sort_order, t1.master_id from om_cache_ordered_master t0 left join om_cache_ordered_detail t1 on t1.master_id = t0.id where t0.id = ? order by t1.sort_order;");
Copy link
Contributor

@rPraml rPraml Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if the master bean is in cache, we take a different code path (defaultBeanLoader) instead of LoadManyRequest and this produces different queries.
IMHO this query is more complex, as it queries two tables.
In the refactored version (See here ) we got the same query


DB.cacheManager().clearAll();
masterDbNew = DB.find(OmCacheOrderedMaster.class, master.getId());
LoggedSql.start();
masterDbNew.getDetails().size();
sql = LoggedSql.stop();
assertThat(sql).hasSize(1).first().asString()
.startsWith("select t0.master_id, t0.id, t0.name, t0.version, t0.sort_order, t0.master_id from om_cache_ordered_detail t0 where (t0.master_id) in (?) order by t0.master_id, t0.sort_order;");

assertThat(masterDbNew.getDetails()).containsExactly(detail2, detail1);
}
}