Skip to content

Commit

Permalink
#2855 - @DBArray not allowing null to be persisted, stores empty list…
Browse files Browse the repository at this point in the history
… 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)`]
  • Loading branch information
rbygrave committed Oct 13, 2022
1 parent 475f937 commit 9a25fb5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ void setDbMap(DeployBeanProperty prop, DbMap dbMap) {
* Set the DbArray type (effectively Postgres only).
*/
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();
ScalarType<?> scalarType = typeManager.getArrayScalarType(type, prop.getGenericType(), prop.isNullable());
if (scalarType == null) {
Expand Down
2 changes: 1 addition & 1 deletion ebean-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
<extensions>true</extensions>
<configuration>
<tiles>
<tile>io.ebean.tile:enhancement:13.6.5</tile>
<tile>io.ebean.tile:enhancement:13.10.0</tile>
</tiles>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ enum Status {
@DbArray
List<Float> floats;

@DbArray
List<Status> statuses;
@DbArray(nullable = false)
List<Status> statuses = new ArrayList<>();

@DbArray
List<VarcharEnum> vcEnums = new ArrayList<>();

@DbArray
List<IntEnum> intEnums = new ArrayList<>();
@DbArray @NotNull
List<IntEnum> intEnums;

@DbArray
Set<Status> status2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void insert() throws SQLException {
DB.find(EArrayBean.class).delete();
bean.setName("some stuff");
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();
phNumbers.add("4321");
Expand Down Expand Up @@ -170,7 +172,26 @@ void insertNulls() {
bean.setOtherIds(null);

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
Expand Down
1 change: 1 addition & 0 deletions ebean-test/src/test/resources/ebean.mf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ entity-packages: org,misc
transactional-packages: org
querybean-packages: none
synthetic: true
allow-nullable-dbarray: true

0 comments on commit 9a25fb5

Please sign in to comment.