3 ms·
Kotlin data classes can be used with the JPA contrary to Java records. It's good to encourage immutability, but it is often a misfit and isn't the role of a "da
by The_rationalist 5y ago
Kotlin data classes can be used with the JPA contrary to Java records.
It's good to encourage immutability, but it is often a misfit and isn't the role of a "data class", they could have added a separate class qualifier immutable that would have been a better separation of concerns, here it's ad-hoc.
Kotlin is enabling such immutable support thanks to Java records ironically https://kotlinlang.org/docs/jvm-records.html#declare-records-in-kotlin https://kotlinlang.org/docs/jvm-records.html#declare-records...
Note however that an upcoming version of Java will get first class ergonomic support for manipulating immutable data:
https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md#withers https://github.com/openjdk/amber-docs/blob/master/eg-drafts/...
There is also https://github.com/hrldcpr/pcollections https://github.com/hrldcpr/pcollections
- vips7L 5y agoI'm really not sure if using data classes with JPA is smart idea. Solely because of generated hashCode/equals and JPA lazy loading. For instance ebean recommends against it [0]. https://ebean.io/docs/best-practice/#kotlin-data-class https://ebean.io/docs/best-practice/#kotlin-data-class
- Bjartr 5y ago> Kotlin is enabling such immutable support thanks to Java records ironically That a member of the JVM ecosystem is leveraging new JVM capabilities isn't ironic, it's totally expected.
- hueho 5y ago> Kotlin data classes can be used with the JPA contrary to Java records. Arguably this is more of an issue of JPA than an strict advantage of Kotlin: JPA was designed at a time where the general consensus was to primarily use mutable data, and was heavily influenced not only by existing Java ORM APIs, but by their implementations. Kotlin itself supports JPA by the use of a compiler plugin, which is a good enough solution, but nevertheless, not one native to the language. Data classes mostly work by accident, but pretty much any documentation you will find points it to _not_ use them with JPA.