mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-13 06:42:57 +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;
|
private OmlBar bar;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
|
@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() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
@@ -13,11 +13,9 @@ public class TestLazyManyViaPath extends BaseTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
|
||||||
OmlBar bar = new OmlBar();
|
OmlBar bar = new OmlBar();
|
||||||
List<OmlFoo> fooList = new ArrayList<>();
|
List<OmlFoo> fooList = new ArrayList<>();
|
||||||
fooList.add(createNewFooWithBar(bar));
|
fooList.add(createNewFooWithBar(bar));
|
||||||
|
|
||||||
bar.setFooList(fooList);
|
bar.setFooList(fooList);
|
||||||
|
|
||||||
DB.save(bar);
|
DB.save(bar);
|
||||||
@@ -30,26 +28,24 @@ public class TestLazyManyViaPath extends BaseTestCase {
|
|||||||
|
|
||||||
// This works
|
// This works
|
||||||
List<OmlFoo> foosList = fooFromDb.getBar().getFooList();
|
List<OmlFoo> foosList = fooFromDb.getBar().getFooList();
|
||||||
assertThat(fooList.size()).isEqualTo(1);
|
assertThat(foosList).hasSize(1);
|
||||||
|
assertThat(fooList).hasSize(1);
|
||||||
|
|
||||||
OmlBaz bazFromDb = DB
|
OmlBaz bazFromDb = DB
|
||||||
.find(OmlBaz.class)
|
.find(OmlBaz.class)
|
||||||
.where()
|
.where()
|
||||||
.eq("id", bar.getFooList().get(0).getBazList().get(0).getId())
|
.eq("id", bar.getFooList().get(0).getBazList().get(0).getId())
|
||||||
.findOne();
|
.findOne();
|
||||||
|
assert bazFromDb != null;
|
||||||
|
|
||||||
// This does not work and gives the exception
|
// This does not work and gives the exception
|
||||||
List<OmlFoo> foosList1 = bazFromDb.getFoo().getBar().getFooList();
|
List<OmlFoo> foosList1 = bazFromDb.getFoo().getBar().getFooList();
|
||||||
assertThat(foosList1.size()).isEqualTo(1);
|
assertThat(foosList1.size()).isEqualTo(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static OmlFoo createNewFooWithBar(OmlBar bar) {
|
private static OmlFoo createNewFooWithBar(OmlBar bar) {
|
||||||
OmlFoo foo = new OmlFoo();
|
OmlFoo foo = new OmlFoo(new OmlBaz());
|
||||||
foo.setBar(bar);
|
foo.setBar(bar);
|
||||||
OmlBaz baz = new OmlBaz();
|
|
||||||
baz.setFoo(foo);
|
|
||||||
foo.getBazList().add(baz);
|
|
||||||
return foo;
|
return foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package io.ebean.test.kotlin
|
||||||
|
|
||||||
|
import io.ebean.common.BeanList
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.example.order.Customer
|
||||||
|
import org.example.order.Order
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class OneToManyEnhancementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `o2m is BeanCollection`() {
|
||||||
|
val customer = Customer("foo")
|
||||||
|
val order = Order(customer)
|
||||||
|
|
||||||
|
assertThat(order.items).isInstanceOf(BeanList::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user