Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jetbrains/jpa-buddy-kotlin-entities
This repository shows how to declare Hibernate entities using Kotlin
https://github.com/jetbrains/jpa-buddy-kotlin-entities
hibernate jpa jpa-buddy jpa-entities kotlin sample
Last synced: 10 days ago
JSON representation
This repository shows how to declare Hibernate entities using Kotlin
- Host: GitHub
- URL: https://github.com/jetbrains/jpa-buddy-kotlin-entities
- Owner: JetBrains
- License: apache-2.0
- Created: 2021-05-20T13:45:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-06T16:46:42.000Z (11 months ago)
- Last Synced: 2025-01-30T02:01:43.011Z (15 days ago)
- Topics: hibernate, jpa, jpa-buddy, jpa-entities, kotlin, sample
- Language: Kotlin
- Homepage:
- Size: 54.7 KB
- Stars: 64
- Watchers: 6
- Forks: 19
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![JetBrains incubator project](https://jb.gg/badges/incubator-flat-square.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
# JPA Entities in Kotlin
This repository was originally used in the "[Getting the Most from JPA with Kotlin](https://www.youtube.com/watch?v=a_6V8xwiv04)" webinar.
To follow the webinar flow step by step, checkout the "[webinar-starting-point](https://github.com/jpa-buddy/kotlin-entities/tree/webinar-starting-point)" branch and click on the image below to start watching.[![Watch the video](https://img.youtube.com/vi/a_6V8xwiv04/hqdefault.jpg)](https://www.youtube.com/watch?v=a_6V8xwiv04)
## 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*](https://kotlinlang.org/docs/all-open-plugin.html) 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`.
```...
all-open
...all-open:annotation=javax.persistence.Entity
all-open:annotation=javax.persistence.MappedSuperclass
all-open:annotation=javax.persistence.Embeddable
```
- 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*](https://kotlinlang.org/docs/no-arg-plugin.html#jpa-support) compiler plugin, which ensures that no-args constructor will be generated in bytecode for each JPA-related class.
```...
jpa
...```
- 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](https://vladmihalcea.com/the-best-way-to-implement-equals-hashcode-and-tostring-with-jpa-and-hibernate/) or [Thorben Janssen](https://thorben-janssen.com/ultimate-guide-to-implementing-equals-and-hashcode-with-hibernate/).
- [JPA Buddy](https://plugins.jetbrains.com/plugin/15075-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](src/main/kotlin/com/jpabuddy/kotlinentities/domain.kt) file. It also includes detailed explanation of the different use cases inlined in the comments.