ToMany collections that are not loaded on freeze are marked unloaded

They are marked as unloaded which means accessing the collection will
throw a PersistenceException.

e.g. customer.getOrders() will throw PersistenceException rather than
return an empty list.
This commit is contained in:
Rob Bygrave
2024-01-30 22:32:49 +13:00
parent bc302b19db
commit 93ae5f2b7c
5 changed files with 18 additions and 6 deletions
@@ -46,7 +46,8 @@ public final class BeanList<E> extends AbstractBeanCollection<E> implements List
@Override
public Object freeze() {
return list == null ? Collections.emptyList() : Collections.unmodifiableList(list);
// null -> illegal to access reference collection
return list == null ? null : Collections.unmodifiableList(list);
}
@Override
@@ -39,7 +39,8 @@ public final class BeanMap<K, E> extends AbstractBeanCollection<E> implements Ma
@Override
public Object freeze() {
return map == null ? Collections.emptyMap() : Collections.unmodifiableMap(map);
// null -> illegal to access reference collection
return map == null ? null : Collections.unmodifiableMap(map);
}
@Override
@@ -42,7 +42,8 @@ public final class BeanSet<E> extends AbstractBeanCollection<E> implements Set<E
@Override
public Object freeze() {
return set == null ? Collections.emptySet() : Collections.unmodifiableSet(set);
// null -> illegal to access reference collection
return set == null ? null : Collections.unmodifiableSet(set);
}
@Override
@@ -1140,6 +1140,10 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> implements ST
if (value instanceof BeanCollection) {
BeanCollection<?> bc = (BeanCollection<?>) value;
setValue(entityBean, bc.freeze());
if (bc.isReference()) {
// make it an error to access the collection (no lazy loading allowed)
entityBean._ebean_getIntercept().setPropertyUnloaded(propertyIndex);
}
}
}
}
@@ -42,9 +42,13 @@ public class TestQueryOrderById extends BaseTestCase {
for (Order order : orders) {
Customer customer = order.getCustomer();
if (cachedCustomerIds.contains(customer.getId())) {
List<Order> orders1 = customer.getOrders();
assertThat(orders1).isEmpty();
assertThat(orders1).isSameAs(Collections.EMPTY_LIST);
// Illegal to access customer.orders as that was not loaded and no lazy loading is allowed
// List<Order> orders1 = customer.getOrders();
// assertThat(orders1).isEmpty();
// assertThat(orders1).isSameAs(Collections.EMPTY_LIST);
List<Contact> contacts = customer.getContacts();
assertThat(contacts).isNotNull();
// customer.setContacts(new ArrayList<>());
// customer.setName("modified");
@@ -118,6 +122,7 @@ public class TestQueryOrderById extends BaseTestCase {
// perform some lazy loading if we desire
for (Customer customer : list) {
customer.getContacts().size();
Address billingAddress = customer.getBillingAddress();
if (billingAddress != null) {
Country country = billingAddress.getCountry();