Skip to content

Commit 88d8b4f

Browse files
committed
Fix orderBy for cached many requests
1 parent 6d5f740 commit 88d8b4f

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultBeanLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ private void loadManyInternal(EntityBean parentBean, String propertyName, boolea
105105
if (ebi.isReadOnly()) {
106106
query.setReadOnly(true);
107107
}
108+
if (many.hasOrderColumn()) {
109+
query.orderBy(many.path() + "." + many.fetchOrderBy());
110+
}
108111

109112
server.findOne(query);
110113
if (beanCollection != null) {

ebean-test/src/test/java/org/tests/cascade/TestOrderedList.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,51 @@ public void testModifyListWithCache() {
188188
assertThat(masterDb.getDetails()).containsExactlyInAnyOrder(detail3, detail1);
189189

190190
}
191+
192+
@Test
193+
public void testModifyListWithCache2() {
194+
final OmCacheOrderedMaster master = new OmCacheOrderedMaster("Master");
195+
final OmCacheOrderedDetail detail1 = new OmCacheOrderedDetail("Detail1");
196+
final OmCacheOrderedDetail detail2 = new OmCacheOrderedDetail("Detail2");
197+
final OmCacheOrderedDetail detail3 = new OmCacheOrderedDetail("Detail3");
198+
DB.save(detail1);
199+
DB.save(detail2);
200+
DB.save(detail3);
201+
master.getDetails().add(detail1);
202+
master.getDetails().add(detail2);
203+
master.getDetails().add(detail3);
204+
205+
DB.save(master);
206+
207+
OmCacheOrderedMaster masterDb = DB.find(OmCacheOrderedMaster.class, master.getId()); // load cache
208+
assertThat(masterDb.getDetails()).containsExactly(detail1, detail2, detail3);
209+
210+
masterDb = DB.find(OmCacheOrderedMaster.class, master.getId());
211+
assertThat(masterDb.getDetails()).containsExactly(detail1, detail2, detail3); // hit cache
212+
213+
// 1 und 2 tauschen
214+
masterDb.getDetails().add(0, masterDb.getDetails().remove(1));
215+
DB.save(masterDb);
216+
217+
masterDb.getDetails().remove(2);
218+
DB.save(masterDb);
219+
220+
LoggedSql.start();
221+
OmCacheOrderedMaster masterDbNew = DB.find(OmCacheOrderedMaster.class, master.getId());
222+
masterDbNew.getDetails().size();
223+
assertThat(masterDbNew.getDetails()).containsExactly(detail2, detail1);
224+
List<String> sql = LoggedSql.stop();
225+
assertThat(sql).hasSize(1).first().asString()
226+
.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;");
227+
228+
DB.cacheManager().clearAll();
229+
masterDbNew = DB.find(OmCacheOrderedMaster.class, master.getId());
230+
LoggedSql.start();
231+
masterDbNew.getDetails().size();
232+
sql = LoggedSql.stop();
233+
assertThat(sql).hasSize(1).first().asString()
234+
.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;");
235+
236+
assertThat(masterDbNew.getDetails()).containsExactly(detail2, detail1);
237+
}
191238
}

0 commit comments

Comments
 (0)