Michael Böiers
04/27/2021, 5:58 AMdata class A(var b: B? = null)
data class B(var a: A? = null)
fun main() {
val a = A()
val b = B(a).also { a.b = it }
println(a) // produces StackOverflow as the two toString impls call each other indefinitely
}
I suppose this is unavoidable - nevertheless, it can kill an application merely by converting a plain Java class into a data class. Any thoughts?Michael Böiers
04/27/2021, 6:05 AMfun toStringFromToString() = id.toString()
This would solve the problem neatly for JPA entities, which is where I encountered it.Michael Böiers
04/27/2021, 6:11 AMfun toShallowString() = id.toString()
The contract could be that this implementation would not call any toString
functions of member variables, but only their toShallowString
implementations instead.elizarov
04/27/2021, 6:14 AMdata
classes?Michael Böiers
04/27/2021, 6:16 AMMichael Böiers
04/27/2021, 6:23 AMJonathan Olsson
04/27/2021, 6:25 AMMichael Böiers
04/27/2021, 6:26 AMJonathan Olsson
04/27/2021, 6:27 AMMichael Böiers
04/27/2021, 6:27 AMMichael Böiers
04/27/2021, 6:29 AMthanksforallthefish
04/27/2021, 6:32 AMIn our use case, they were JPA entities. It seemed like best practice to make those data classes, inspired by a Baeldung article. From a usability standpoint, mainly the copy and equals implementations come in handy.this part I really don’t get it though, I don’t feel data classes marry well with jpa. for once, I would consider equality more id-based than content-based. the official spring tutorial (https://spring.io/guides/tutorials/spring-boot-kotlin/) in the jpa section also mention
Here we don't use data classes with val properties because JPA is not designed to work with immutable classes or the methods generated automatically by data classes
Michael Böiers
04/27/2021, 6:33 AMMichael Böiers
04/27/2021, 6:43 AMdeviant
05/06/2021, 8:48 AMthanksforallthefish
05/06/2021, 9:06 AM