Search code examples
postgresqlkotlinspring-dataspring-data-jdbc

Null-values for embedded objects are not accepted


I am trying to persist an object containing an embedded one using Spring Data JDBC. And it is not saved because it does not pass a null-check.

I created an outer class to persist.

data class Outer(
    @Id 
    val id: Long,

    val value: String,

    @Embedded 
    inner: Inner? = null
)
data class Inner(
    val innerValue1: String,
    val innerValue2: String
)

In the DB I would like to have only one table for Outer which contains fields id, value, inner_value_1 and inner_value_2. That's why I put @Embedded annotation on the inner-field.

id and value should be obligatory fields. innver_value_1 and invver_value_2 might be null,

The problem appears when I try to save null-inner object (which is nullable both in Kotlin and in the DB).

val outer = Outer(15, "value")

repo.save(outer) //throws an exception

The saving to repo operation throws an exception cause expects inner not to be null:

Caused by: java.lang.IllegalArgumentException: Target bean must not be null!
    at org.springframework.util.Assert.notNull(Assert.java:198)
    at org.springframework.data.mapping.model.BasicPersistentEntity.verifyBeanType(BasicPersistentEntity.java:550)
    at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:453)

The difficulty is that in Kotlin I cannot pass an object with null-valued-fields (because they are not nullable).

Is there any way to save an empty/null embedded object?


Solution

  • This issue is documented in DATAJDBC-364 and fixed for the 1.1 M4 milestone. Please note that this is related to DATAJDBC-374 which will make it configurable how to load those embedded entities.