mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
#2404 - Initializing @OneToMany with mutableListOf() in Kotlin results in ClassCastException
This commit is contained in:
1 parent
2cee03bc4b
commit
40492f487c
4 files changed
+57
-9
No files matched your search
@@ -0,0 +1,29 @@
|
||||
package org.tests.o2m;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.common.BeanList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.json.EBasicJsonMap;
|
||||
import org.tests.o2m.lazy.OmlBaz;
|
||||
import org.tests.o2m.lazy.OmlFoo;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestOneToManyEnhancement extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
void test_when_constructorAddsEntry() {
|
||||
OmlFoo foo = new OmlFoo(new OmlBaz());
|
||||
|
||||
assertThat(foo.getBazList()).isInstanceOf(BeanList.class);
|
||||
assertThat(foo.getBazList()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_commonCase() {
|
||||
EBasicJsonMap bean = new EBasicJsonMap();
|
||||
|
||||
assertThat(bean.getDetails()).isInstanceOf(BeanList.class);
|
||||
assertThat(bean.getDetails()).isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,12 @@ public class OmlFoo {
|
||||
private OmlBar bar;
|
||||
|
||||
@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
|
||||
private List<OmlBaz> bazList = new ArrayList<OmlBaz>();
|
||||
private List<OmlBaz> bazList = new ArrayList<>();
|
||||
|
||||
public OmlFoo(OmlBaz baz) {
|
||||
bazList.add(baz);
|
||||
baz.setFoo(this);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
|
||||
@@ -13,11 +13,9 @@ public class TestLazyManyViaPath extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
OmlBar bar = new OmlBar();
|
||||
List<OmlFoo> fooList = new ArrayList<>();
|
||||
fooList.add(createNewFooWithBar(bar));
|
||||
|
||||
bar.setFooList(fooList);
|
||||
|
||||
DB.save(bar);
|
||||
@@ -30,26 +28,24 @@ public class TestLazyManyViaPath extends BaseTestCase {
|
||||
|
||||
// This works
|
||||
List<OmlFoo> foosList = fooFromDb.getBar().getFooList();
|
||||
assertThat(fooList.size()).isEqualTo(1);
|
||||
assertThat(foosList).hasSize(1);
|
||||
assertThat(fooList).hasSize(1);
|
||||
|
||||
OmlBaz bazFromDb = DB
|
||||
.find(OmlBaz.class)
|
||||
.where()
|
||||
.eq("id", bar.getFooList().get(0).getBazList().get(0).getId())
|
||||
.findOne();
|
||||
assert bazFromDb != null;
|
||||
|
||||
// This does not work and gives the exception
|
||||
List<OmlFoo> foosList1 = bazFromDb.getFoo().getBar().getFooList();
|
||||
assertThat(foosList1.size()).isEqualTo(1);
|
||||
|
||||
}
|
||||
|
||||
private static OmlFoo createNewFooWithBar(OmlBar bar) {
|
||||
OmlFoo foo = new OmlFoo();
|
||||
OmlFoo foo = new OmlFoo(new OmlBaz());
|
||||
foo.setBar(bar);
|
||||
OmlBaz baz = new OmlBaz();
|
||||
baz.setFoo(foo);
|
||||
foo.getBazList().add(baz);
|
||||
return foo;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user