How to gracefully transform entity into DTO in Kotlin?

Viewed 547

I am working on Kotlin + SpringBoot web service, in which I want to transform DTOs into entities in the most convenient way.

Entities:

@Entity
data class Shop(
   @Id
   @GeneratedValue
   val id: Long,
   val name: String
   @OneToOne
   val owner: User,
   ...
)

@Entity
data class User(
   @Id
   @GeneratedValue
   val id: Long,
   val name: String,
   ...
)

DTO:

data class ShopDTO(
   val id: Long,
   val name: String,
   val ownerId: Long,
   val ownerName: String,
   ...
)

So when someone wants to create a new Shop, my service gets a ShopDTO(name, ownerId) as request body, then I need to transform it into Shop object to be able to save it to the DB. Now here is how my mapper function looks like:

fun fromDTO(source: ShopDTO) = Shop(
   id = source.id,
   name = source.name,
   owner = ???,
   ...
)

To be able to store a Shop with an owner I only need an id. It would be enough to create a new User with the given ownerId. To achive this I tried these solutions:

  • Add default value to the fields in the User class.
  • Make the fields nullable.
  • Add a secondary constructor. This also needs default values.
  • Use some reflection magic to create an empty object and then set the id.
  • Call a findById method on the UserRepository with the given id.

I want to keep the non-null, immutable fields of my entities and do not want to use reflection. Also do not want to run an unnecessary select DB query just to get back the user by the id.

Could you please suggest me other options? How would you handle this situation? Is there any good mapper framework in Kotlin which can solve this problem?

0 Answers
Related