@ElementCollection inside @Embeddable causes issues with Boolean column

Description

The following kotlin code works fine:
@Entity
data class P(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,

@Column(nullable = false)
val name: String,

@Embedded
val sub: Sub? = null
)

@Embeddable
data class Sub(
@Column
var sc1: Boolean = false
)

There is a single table created with both P and Sub columns:

Hibernate: create table p (id bigint generated by default as identity, name varchar(255) not null, sc1 boolean, primary key (id))

and the following test code:

val p = P(name = "aa")
pRepo.save(p)
return pRepo.findByName("aa")

returns (as expected):
{
"id": 1,
"name": "aa",
"sub": null
}

The problem is adding an @EntityCollection to Sub:

@Embeddable
data class Sub(
@Column
var sc1: Boolean = false,
@ElementCollection(fetch = FetchType.EAGER)
var sc2: List<String> = emptyList()
)

The same exact p table is created, and also an additional p_sc2 table with a foreign key constraint (all as expected):

Hibernate: create table p (id bigint generated by default as identity, name varchar(255) not null, sc1 boolean, primary key (id))
Hibernate: create table p_sc2 (p_id bigint not null, sc2 varchar(255))
Hibernate: alter table p_sc2 add constraint FKodcprs91i6g27dxtaxyq17d35 foreign key (p_id) references p

Running the test code adds a row to p, but findByName throws an exception:

Caused by: org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.citi.prism.testClasses.Sub.sc1] of primitive type setter of com.citi.prism.testClasses.Sub.sc1

sc1 is not a primitive type and there's been no change to it's declaration.

Making sc2 nullable fixes the (misleading) exception:

@Embeddable
data class Sub(
@Column
var sc1: Boolean? = false,
@ElementCollection(fetch = FetchType.EAGER)
var sc2: List<String> = emptyList()
)

The generated table is exactly the same:

Hibernate: create table p (id bigint generated by default as identity, name varchar(255) not null, sc1 boolean, primary key (id))

However now running the test code returns:
{
"id": 1,
"name": "aa",
"sub": {
"sc1": null,
"sc2": []
}
}

So the sub field is populated by a default Sub object instead of being null as in the beginning.

Environment

None

Assignee

Unassigned

Reporter

kaj gol