Delegates initialized in the constructor

Delegated properties are really useful for doing some magic with JPA entities e.g. mapping a single currency code column and multiple decimal columns to multiple MonetaryAmount instances.

Sadly, if there are any constructor parameters for the entity class, the initialization of the generated delegate objects in bytecode is compiled into the constructor call of the entity and not a simple field initialization like when there are no arguments in the constructor. Since Hibernate needs to generate proxy objects, the JPA noarg maven plugin generates a no-arg constructor, so the delegate objects are not initialized and the first access to then leads to a null pointer exception.

Is there any workaround beside having no parameters in the constructor and using lateinit non-null vars for all of my properties? Maybe to force the compiler to generate a field initialization or the no-arg plugin to initialize the delegates in the no-arg constructor?

1 Like

Greetings! Did you find a workaround to this?

Does it help if you use invokeInitializers option as described in this issue: https://youtrack.jetbrains.com/issue/KT-36944?

Hi! I see that I missed that in the documentation, it worked perfectly! :slight_smile: Thanks!

1 Like