#2855 - @DbArray not allowing null to be persisted, stores empty list instead

Currently, this bug fix needs to be enabled explicitly via ebean.mf with:

allow-nullable-dbarray: true

We will look to enable this bug fix by default shortly noting that some code
will see a behaviour change. We need to publicise it well when we do turn
this on by default.

This bug fix is actually in ebean-agent 13.10.0 and all this change in ebean-core
does is read the new `@DbArray(nullable=false)` attribute [as a new alternative to
using `@NotNull` or `@Column(nullable=false)`]
This commit is contained in:
Rob Bygrave committed 2022-10-14 11:14:12 +13:00
1 parent 475f937421
commit 9a25fb584c
5 files changed
+33 -6

No files matched your search

@@ -171,6 +171,11 @@ public final class DeployUtil {
* Set the DbArray type (effectively Postgres only). * Set the DbArray type (effectively Postgres only).
*/ */
void setDbArray(DeployBeanProperty prop, DbArray dbArray) { void setDbArray(DeployBeanProperty prop, DbArray dbArray) {
if (!dbArray.nullable()) {
// Non-nullable ScalarTypeArray's will auto bind null to empty array, we need to
// set nullable(false) before the ScalarTypeArray is determined and assigned
prop.setNullable(false);
}
Class<?> type = prop.getPropertyType(); Class<?> type = prop.getPropertyType();
ScalarType<?> scalarType = typeManager.getArrayScalarType(type, prop.getGenericType(), prop.isNullable()); ScalarType<?> scalarType = typeManager.getArrayScalarType(type, prop.getGenericType(), prop.isNullable());
if (scalarType == null) { if (scalarType == null) {
+1 -1
View File
@@ -272,7 +272,7 @@
<extensions>true</extensions> <extensions>true</extensions>
<configuration> <configuration>
<tiles> <tiles>
<tile>io.ebean.tile:enhancement:13.6.5</tile> <tile>io.ebean.tile:enhancement:13.10.0</tile>
</tiles> </tiles>
</configuration> </configuration>
</plugin> </plugin>
@@ -46,14 +46,14 @@ public class EArrayBean {
@DbArray @DbArray
List<Float> floats; List<Float> floats;
@DbArray @DbArray(nullable = false)
List<Status> statuses; List<Status> statuses = new ArrayList<>();
@DbArray @DbArray
List<VarcharEnum> vcEnums = new ArrayList<>(); List<VarcharEnum> vcEnums = new ArrayList<>();
@DbArray @DbArray @NotNull
List<IntEnum> intEnums = new ArrayList<>(); List<IntEnum> intEnums;
@DbArray @DbArray
Set<Status> status2; Set<Status> status2;
@@ -28,6 +28,8 @@ class TestDbArray_basic extends BaseTestCase {
DB.find(EArrayBean.class).delete(); DB.find(EArrayBean.class).delete();
bean.setName("some stuff"); bean.setName("some stuff");
assertThat(bean.getStatuses()).as("DbArray is auto initialised").isNotNull(); assertThat(bean.getStatuses()).as("DbArray is auto initialised").isNotNull();
assertThat(bean.getIntEnums()).as("DbArray is auto initialised").isNotNull();
assertThat(bean.getUids()).as("DbArray is auto initialised").isNotNull();
List<String> phNumbers = bean.getPhoneNumbers(); List<String> phNumbers = bean.getPhoneNumbers();
phNumbers.add("4321"); phNumbers.add("4321");
@@ -170,7 +172,26 @@ class TestDbArray_basic extends BaseTestCase {
bean.setOtherIds(null); bean.setOtherIds(null);
DB.save(bean); DB.save(bean);
DB.delete(bean);
EArrayBean found = DB.find(EArrayBean.class, bean.getId());
assertThat(found.getPhoneNumbers()).isNull();
assertThat(found.getStatuses()).isEmpty();
assertThat(found.getIntEnums()).isEmpty();
assertThat(found.getUids()).isEmpty();
found.setName("some nulls 2");
DB.save(found);
EArrayBean found2 = DB.find(EArrayBean.class)
.where()
.eq("id", bean.getId())
.isNull("phoneNumbers")
.findOne();
assertThat(found2).isNotNull();
assertThat(found2.getPhoneNumbers()).isNull();
DB.delete(found);
} }
@Test @Test
+1
View File
@@ -4,4 +4,5 @@ entity-packages: org,misc
transactional-packages: org transactional-packages: org
querybean-packages: none querybean-packages: none
synthetic: true synthetic: true
allow-nullable-dbarray: true