Search code examples
jpakotlinkotlin-multiplatform

How to use JPA annotations in a kotlin/multiplatform project?


I want to implement a project using kotlin/multiplatform consisting of a backend on the jvm and a web-app in js. The structure would be like this:

root
 |- webapp (kotlin/js using kotlin-react)
 |- shared (kotlin/multiplatform for shared data)
 |- server (kotlin/jvm using micronauts)

The data classes used by the applications belong in the shared project, but to use jpa I need jvm-annotations.

A solution would be to not use kotlin data-classes and inherit in the jvm. I also tried to implement the jpa annotations using the experimental @OptionalExpectation but that went nowhere since:

  • they require non-annotation-types when used with typealias which can't be implemented with @OptionalExpectation.
  • let the multiplatform-annotations inherit from the multiplatform-annotations isn't possible since kotlin doesn't yet support annotation inheritance.

Should I refrain from using the data-class feature and use inheritance or is there a more elegant way?


Solution

  • I think in general model classes shouldn't be shared between different applications, with one of the the exceptions being applications that use the same data source.

    If you want to share data structures between the Server and the Web app I would suggest creating DTO classes specifically for that.

    A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an application to another.