Code Monkey home page Code Monkey logo

jpa-buddy-kotlin-entities's Introduction

JetBrains incubator project

JPA Entities in Kotlin

This repository was originally used in the "Getting the Most from JPA with Kotlin" webinar. To follow the webinar flow step by step, checkout the "webinar-starting-point" branch and click on the image below to start watching.

Watch the video

JPA Entities on Kotlin Checklist

  • To avoid significant performace issues and enable lazy loading for Many/One to One assosiations make sure you mark all JPA-related classes and their properties as open.
@Table(name = "project")
@Entity
open class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    open var id: Long? = null

    @Column(name = "name", nullable = false)
    open var name: String? = null

    ...
}
  • You may use the all-open compiler plugin to make all JPA-related classes and properties open by default. Make sure you configure it right, so it applys for all classes annotated as @Entity, @MappedSuperclass, @Embeddable.
<compilerPlugins>
   ...
   <plugin>all-open</plugin>
   ...
</compilerPlugins>
<pluginOptions>
   <option>all-open:annotation=javax.persistence.Entity</option>
   <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
   <option>all-open:annotation=javax.persistence.Embeddable</option>
</pluginOptions> 
  • Using primary constructors in JPA-related classes will cause the following exception org.hibernate.InstantiationException: No default constructor for entity. To resolve this issue you may manually define no-args constructor or use the kotlin-jpa compiler plugin, which ensures that no-args constructor will be generated in bytecode for each JPA-related class.
<compilerPlugins>
   ...
   <plugin>jpa</plugin>
   ...   
</compilerPlugins>
  • To use data classes
    • Enable the all-open plugin as it was described above, because this is the only way to make data classes open in the compiled bytecode.
    • Override equals(), hashCode(), toString() in accordance with one of this articles by Vlad Mihalcea or Thorben Janssen.
  • JPA Buddy is aware of all these things and always generate valid entities for you, including extra stuff like equals(), hashCode(), toString()!

Find more examples of JPA entities on Kotlin in the domain.kt file. It also includes detailed explanation of the different use cases inlined in the comments.

jpa-buddy-kotlin-entities's People

Contributors

aleksey-stukalov avatar jsebasct avatar ludeknovy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.