Skip to content

Commit b85764c

Browse files
committed
Add a section about immutable classes persistence in Kotlin refdoc
1 parent 74120ef commit b85764c

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/docs/asciidoc/kotlin.adoc

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,9 @@ functions of Spring beans proxified with CGLIB such as `@Configuration` classes.
353353

354354
Fortunately, Kotlin 1.0.6+ now provides a
355355
https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin[`kotlin-spring`]
356-
plugin that automatically opens classes and their member functions for types
357-
annotated or meta-annotated with one of the following annotations:
356+
plugin, a preconfigured version of `kotlin-allopen` plugin that automatically opens classes
357+
and their member functions for types annotated or meta-annotated with one of the following
358+
annotations:
358359

359360
* `@Component`
360361
* `@Async`
@@ -367,6 +368,33 @@ annotations are meta-annotated with `@Component`.
367368

368369
http://start.spring.io/#!language=kotlin[start.spring.io] enables it by default.
369370

371+
=== Using immutable class instances for persistence
372+
373+
In Kotlin, it is very convenient and a best practice to declare read-only properties within
374+
the primary constructor, as in the following example:
375+
376+
[source,kotlin]
377+
----
378+
class Person(val name: String, val age: Int)
379+
----
380+
381+
But some persistence technologies like JPA require a default constructor, preventing this
382+
kind of design. Fortunately, there is now a workaround for this
383+
https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell["default constructor hell"]
384+
since Kotlin provides a https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-jpa-compiler-plugin[kotlin-jpa]
385+
plugin which generates synthetic no-arg constructor for classes annotated with JPA annotations.
386+
387+
If you need to leverage this kind of mechanism for other persistence technologies, you can
388+
configure https://kotlinlang.org/docs/reference/compiler-plugins.html#how-to-use-no-arg-plugin[kotlin-noarg]
389+
plugin.
390+
391+
[NOTE]
392+
====
393+
As of Kay release train, Spring Data supports Kotlin immutable class instances and should
394+
not require `kotlin-noarg` plugin if the underlying persistence solution allows it
395+
(for example with MongoDB).
396+
====
397+
370398
=== Injecting dependencies
371399

372400
Our recommendation is to try and favor constructor injection with `val` read-only (and non-nullable when possible)
@@ -562,8 +590,8 @@ Spring WebFlux is recommended if one wants to create applications that will deal
562590
long-lived connections, streaming scenarios or simply if one wants to use the web functional
563591
Kotlin DSL.
564592

565-
For other use cases, Spring MVC and its annotation-based programming model is a perfectly
566-
valid and fully supported choice.
593+
For other use cases, especially if you are using blocking technologies like JPA, Spring
594+
MVC and its annotation-based programming model is a perfectly valid and fully supported choice.
567595

568596
[[resources-started]]
569597
== Resources

0 commit comments

Comments
 (0)