Skip to content

@Embedded.Nullable returns as non-null for null collection property #1737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
radekjezdik opened this issue Feb 11, 2024 · 5 comments
Closed
Assignees
Labels
type: bug A general bug

Comments

@radekjezdik
Copy link

Given an @Embedded.Nullable on a property whose object contains a property with a collection type, e.g. list (backed by a varchar array in the DB), the annotated object is returned non-null even for a null value of this collection.

data class Entity(
    @Embedded.Nullable
    val embedded: EmbeddedObject?
) {
    @Id
    var id: Long? = null
}

data class EmbeddedObject(
    @Column("values")
    val values: List<String>?
)

For a row with values = null, the mapper results in the embedded property being a non-null object, and its property values is null.

I guess this is a bug, as it goes against the documented behavior:

the load strategy for the embedded object if all contained fields yield null values.

which should hold for an embedded with a single collection field yielding a null value.

version: 3.2.2

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 11, 2024
@mp911de
Copy link
Member

mp911de commented Feb 12, 2024

Placing collections in an embeddable is somewhat problematic as the current code only checks values from the current row in ResultSet. To determine whether there are values, we would have to run a query to check whether there are values. Then run another query to fetch the actual rows. With a single property, all this can be done, but running multiple queries for multiple relationship properties is where it becomes problematic.

I agree this is a bug and we need to take care of it.

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 12, 2024
@radekjezdik
Copy link
Author

Maybe I am lost in the context, but the collection is part of the current row when it is an array column. So, no additional query should be needed.

The code imho already has all the information needed to return the correct null result for the embedded object, as it needed to fill all the null fields in it already.

@mp911de
Copy link
Member

mp911de commented Feb 12, 2024

You're right; I missed that the component type is String and, therefore, a simple type. In any case, we nee to fix the issue on our side.

@fantnk
Copy link

fantnk commented May 15, 2024

We also faced this problem. Do you have a workaround for that?

@schauder schauder self-assigned this Jun 7, 2024
schauder added a commit that referenced this issue Jun 10, 2024
Embedded entities which contain a empty collection of values that aren't entities are now considered empty, if this collection is empty.

Closes #1737
mp911de added a commit that referenced this issue Jun 11, 2024
Use ObjectUtils.isEmpty for emptiness check.

See #1737
Original pull request: #1812
mp911de pushed a commit that referenced this issue Jun 11, 2024
… holds entities.

Embedded entities which contain a empty collection of values that aren't entities are now considered empty, if this collection is empty.

Closes #1737
Original pull request: #1812
mp911de added a commit that referenced this issue Jun 11, 2024
Use ObjectUtils.isEmpty for emptiness check.

See #1737
Original pull request: #1812
mp911de pushed a commit that referenced this issue Jun 11, 2024
… holds entities.

Embedded entities which contain a empty collection of values that aren't entities are now considered empty, if this collection is empty.

Closes #1737
Original pull request: #1812
mp911de added a commit that referenced this issue Jun 11, 2024
Use ObjectUtils.isEmpty for emptiness check.

See #1737
Original pull request: #1812
@Tree4Free
Copy link

Tree4Free commented Jun 25, 2024

After upgrading to 3.3.1 I encounter an issue that is probably related to this change:
Basically when fetching a list property it is returned as null when it is an empty list.

Using a combination of Java + Kotlin:
My Entity: (Java + Lombok)

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class MyTable extends Entity {
    List<String> tags;
}

My database table (postgres):

create table my_table
(
    id uuid not null,
    version bigint not null,
    tags text[] not null,
    constraint pk_my_table primary key (id)
);

My test case (Kotlin)

@Repository
interface MyTableRepository: CrudRepository<MyTable, UUID>, PagingAndSortingRepository<MyTable, UUID>

@Test
fun test() {
    val instance = myTableRepository.save(MyTable(null, 0, emptyList())) // instance.tags is still an empty list
    assertNotNull(myTableRepository.findById(instance.id).get().tags) // data from db tags is null
}

(I will create a seperate issue for this later this evening)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
6 participants