https://kotlinlang.org logo
#spring
Title
s

ShootingStar

04/14/2021, 5:07 PM
Hi Guys, In Kotlin, is there a good reason I have to use immutable class for JPA entities? My colleague said he prefer to design like that and it is a good way to reduce some side-effects. However, I don’t believe it is a good way. I think JPA entities are born with a characteristic that its properties will be update one day. I read the persistence-with-jpa section in the spring boot kotlin tutorail and I show it to my colleague but he said he could not agree with it. What’s your opinion?
s

sdeleuze

04/14/2021, 5:09 PM
I think JPA is not designed for immutable JPA entities, we had long discussions about that in the past with various experts including in the Spring Data JPA team.
I don't remember the details, but even if that can be frustrating from a Kotlin developer POV, I don't think that's a good idea.
I think @jbnizet was involved in those discussions as well.
s

ShootingStar

04/14/2021, 5:17 PM
Thank you so much @sdeleuze BTW, I saw many your conference speech and blog articles. Truly, I learnt a lot from it. Thank you again 😉
s

sdeleuze

04/14/2021, 5:17 PM
Thanks 🙂
👍 1
n

nfrankel

04/14/2021, 5:19 PM
true, jpa entities are meant to be mutable but... if you’re in a web context, they probably aren’t what you do: you load them from the database and display them on the web page after serialization or you deserialize them from forms or whatever most of the times, i believe they can be immutable
s

ShootingStar

04/14/2021, 5:24 PM
Thank you @nfrankel. I think I have the both cases. Usually, I made a common module which have JPA entities and a rest api module and admin api module will be use the common module. I am not sure I have to made two entity classes for the admin and for the api
n

nfrankel

04/14/2021, 5:25 PM
i wouldn’t go that way make your life easy 🙂 usage trumps perfect design all the time
s

sdeleuze

04/14/2021, 5:26 PM
We quickly faced some issues on update on not so complex web apps.
It is not about perfect design, it is about using the right construct with the right tool. With no constraints Kotlin favor immutable data classes. JPA is not designed for that and Spring Data JPA is not as well. Kotlin idioms should not be applied blindly.
s

ShootingStar

04/14/2021, 5:28 PM
I can’t say thank you enough @nfrankel @sdeleuze This discussion is really helpful for me. I will follow your advices 🙂
👍 1
c

christophsturm

04/14/2021, 7:13 PM
i have been working on a immutable ORM for quite some time, and I'm still not sure if immutability and databases go so well together. are there other immutable orms?
s

sdeleuze

04/14/2021, 7:15 PM
Not really an ORM but you can use immutable classes with Spring Data JDBC. I don’t know jOOQ enough to answer about it.
c

christophsturm

04/14/2021, 7:27 PM
iirc jooq is also more a sql dsl than an orm
2 Views