mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 16:47:18 +00:00
#879 - Ebean not fetching complete list of attribute beans when equals(Object o) is implemented
This commit is contained in:
@@ -85,11 +85,23 @@ public final class BeanList<E> extends AbstractBeanCollection<E> implements List
|
||||
|
||||
@Override
|
||||
public void internalAddWithCheck(Object bean) {
|
||||
if (list == null || !list.contains(bean)) {
|
||||
if (list == null || !containsInstance(bean)) {
|
||||
internalAdd(bean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains using instance equality for List (specifically not .equals() based).
|
||||
*/
|
||||
private boolean containsInstance(Object bean) {
|
||||
for (Object element : list) {
|
||||
if (element == bean) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean checkEmptyLazyLoad() {
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
|
||||
@@ -230,4 +230,51 @@ public class BeanListTest {
|
||||
assertThat(list.getModifyRemovals()).containsExactly(object1);
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void internalAddWithCheck_when_interestingEquals_usesInstanceEquality() {
|
||||
|
||||
BeanList<Object> list = new BeanList<>();
|
||||
assertThat(list).hasSize(0);
|
||||
|
||||
SomeBean a = new SomeBean("A");
|
||||
list.internalAddWithCheck(a);
|
||||
list.internalAddWithCheck(a);
|
||||
assertThat(list).hasSize(1);
|
||||
|
||||
// expect to ignore equals and add as it is a diff instance (aka don't use equals())
|
||||
list.internalAddWithCheck(new SomeBean("A"));
|
||||
assertThat(list).hasSize(2);
|
||||
|
||||
list.internalAddWithCheck(new SomeBean("B"));
|
||||
assertThat(list).hasSize(3);
|
||||
}
|
||||
|
||||
/**
|
||||
* A entity bean with interesting equals implementation.
|
||||
*/
|
||||
private static class SomeBean {
|
||||
|
||||
final String val;
|
||||
|
||||
SomeBean(String val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other instanceof SomeBean) {
|
||||
return this.val.equals(((SomeBean)other).val);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user