Assuming I have a data class like this
data class PersonDTO(
val id: Long?,
val firstName: String,
val lastName: String,
)
which I then persist in the database on this entity
@Entity
@Table(name = "person")
class Person(
@Column(name = "first_name", nullable = true)
var firstName: String? = null,
@Column(name = "last_name", nullable = true)
var lastName: String? = null,
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
}
like so
personRepository.save(
Person(
firstName = personDto.firstName,
lastName = personDto.lastName
)
)
Since firstName
and lastName
are nullable
, I want the data class
PersonDTO
to behave the following way: If firstName
is of length === 0
, return null for firstName
(and the same for lastName
).
Is there a way to achieve this?