Skip to content

Commit

Permalink
Merge pull request #451 from SeongYunKim/feature/eclipselink-support
Browse files Browse the repository at this point in the history
support module for eclipselink (jakarta, javax)
  • Loading branch information
shouwn authored Sep 8, 2023
2 parents 9405b97 + feadc78 commit b540742
Show file tree
Hide file tree
Showing 38 changed files with 2,213 additions and 0 deletions.
38 changes: 38 additions & 0 deletions example/eclipselink/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
plugins {
alias(libs.plugins.kotlin.noarg)
alias(libs.plugins.kotlin.allopen)
alias(libs.plugins.kotlin.jpa)
}

dependencies {
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
implementation(projects.example)
implementation(projects.jpqlDsl)
implementation(projects.jpqlRender)
implementation(projects.eclipselinkSupport)

implementation(libs.logback)
implementation(libs.eclipselink.v4)

runtimeOnly(libs.test.h2)

testFixturesImplementation(libs.eclipselink.v4)
testFixturesImplementation(projects.jpqlRender)
}

kotlin {
jvmToolchain(17)
}

noArg {
annotation("com.linecorp.kotlinjdsl.example.eclipselink.annotation.CompositeId")
}

allOpen {
annotation("com.linecorp.kotlinjdsl.example.eclipselink.annotation.CompositeId")
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.Embeddable")
}

tasks.withType<PublishToMavenRepository>().configureEach { enabled = false }
tasks.withType<PublishToMavenLocal>().configureEach { enabled = false }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.linecorp.kotlinjdsl.example.eclipselink.annotation

annotation class CompositeId
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@file:Suppress("JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.author

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.util.*

@Entity
@Table(name = "author")
class Author(
@Id
@Column(name = "author_id")
val authorId: Long,

@Column(name = "name")
var name: String,
) {
override fun equals(other: Any?): Boolean = Objects.equals(authorId, (other as? Author)?.authorId)
override fun hashCode(): Int = Objects.hashCode(authorId)

override fun toString(): String = "Author(authorId=$authorId)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@file:Suppress("LeakingThis", "unused", "JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.book

import jakarta.persistence.AttributeOverride
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.OneToMany
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.time.OffsetDateTime
import java.util.*

@Entity
@Table(name = "book")
class Book(
@Id
@Column(name = "isbn")
val isbn: String,

@Column(name = "title")
var title: String,

@Column(name = "image_url")
var imageUrl: String,

@Embedded
@AttributeOverride(name = "value", column = Column(name = "price"))
var price: BookPrice,

@Embedded
@AttributeOverride(name = "value", column = Column(name = "sale_price"))
var salePrice: BookPrice,

@Column(name = "publish_date")
var publishDate: OffsetDateTime,

@OneToMany(mappedBy = "book", cascade = [CascadeType.ALL], orphanRemoval = true)
val authors: MutableSet<BookAuthor>,

@OneToOne(mappedBy = "book", cascade = [CascadeType.ALL], orphanRemoval = true)
val publisher: BookPublisher,
) {
init {
authors.forEach { it.book = this }
publisher.book = this
}

override fun equals(other: Any?): Boolean = Objects.equals(isbn, (other as? Book)?.isbn)
override fun hashCode(): Int = Objects.hashCode(isbn)

override fun toString(): String = "Book(isbn=$isbn)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@file:Suppress("JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.book

import com.linecorp.kotlinjdsl.example.eclipselink.annotation.CompositeId
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.IdClass
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import java.io.Serializable
import java.util.*

@Entity
@Table(name = "book_author")
@IdClass(BookAuthor.BookAuthorId::class)
class BookAuthor(
@Id
@Column(name = "author_id")
val authorId: Long,
) {
@Id
@ManyToOne
@JoinColumn(name = "isbn")
lateinit var book: Book

private val bookAuthorId get() = BookAuthorId(book.isbn, authorId)

override fun equals(other: Any?): Boolean =
Objects.equals(bookAuthorId, (other as? BookAuthor)?.bookAuthorId)

override fun hashCode(): Int =
Objects.hashCode(bookAuthorId)

override fun toString(): String =
"BookAuthor(bookAuthorId=$bookAuthorId)"

@CompositeId
data class BookAuthorId(
val book: String,
val authorId: Long,
) : Serializable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@file:Suppress("unused")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.book

import jakarta.persistence.Column
import jakarta.persistence.Embeddable
import java.math.BigDecimal

@Embeddable
data class BookPrice(
@Column(name = "price", scale = 2)
val value: BigDecimal,
)

fun BookPrice(int: Int): BookPrice {
return BookPrice(BigDecimal.valueOf(int.toLong()).setScale(2))
}

fun BookPrice(double: Double): BookPrice {
return BookPrice(BigDecimal.valueOf(double).setScale(2))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@file:Suppress("JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.book

import com.linecorp.kotlinjdsl.example.eclipselink.annotation.CompositeId
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.IdClass
import jakarta.persistence.JoinColumn
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.io.Serializable
import java.util.*

@Entity
@Table(name = "book_publisher")
@IdClass(BookPublisher.BookPublisherId::class)
class BookPublisher(
@Id
@Column(name = "publisher_id")
val publisherId: Long,
) {
@Id
@OneToOne
@JoinColumn(name = "isbn")
lateinit var book: Book

private val bookPublisherId get() = BookPublisherId(book.isbn, publisherId)

override fun equals(other: Any?): Boolean =
Objects.equals(bookPublisherId, (other as? BookPublisher)?.bookPublisherId)

override fun hashCode(): Int =
Objects.hashCode(bookPublisherId)

override fun toString(): String =
"BookPublisher(bookPublisherId=$bookPublisherId)"

@CompositeId
data class BookPublisherId(
val book: String,
val publisherId: Long,
) : Serializable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.linecorp.kotlinjdsl.example.eclipselink.entity.book

import jakarta.persistence.Column
import jakarta.persistence.Embeddable
import java.io.Serializable

/**
* International Standard Book Number
*/
@Embeddable
data class Isbn(
@Column(name = "isbn")
val value: String,
) : Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@file:Suppress("JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.department

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import java.util.*

@Entity
@Table(name = "department")
class Department(
@Id
@Column(name = "department_id")
val departmentId: Long,

@Column(name = "name")
val name: String,
) {
override fun equals(other: Any?): Boolean = Objects.equals(departmentId, (other as? Department)?.departmentId)
override fun hashCode(): Int = Objects.hashCode(departmentId)

override fun toString(): String = "Department(departmentId=$departmentId)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@file:Suppress("LeakingThis", "unused", "JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.employee

import jakarta.persistence.Column
import jakarta.persistence.DiscriminatorColumn
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Inheritance
import jakarta.persistence.InheritanceType
import jakarta.persistence.OneToMany
import jakarta.persistence.Table
import java.util.*

@Entity
@Table(name = "employee")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "employee_type")
class Employee(
@Id
@Column(name = "employee_id")
val employeeId: Long,

@Column(name = "name")
var name: String,

@Column(name = "nickname")
var nickname: String?,

@Column(name = "phone")
var phone: String,

@Embedded
val address: EmployeeAddress,

@OneToMany(mappedBy = "employee")
val departments: MutableSet<EmployeeDepartment>,
) {
init {
departments.forEach { it.employee = this }
}

override fun equals(other: Any?): Boolean = Objects.equals(employeeId, (other as? Employee)?.employeeId)
override fun hashCode(): Int = Objects.hashCode(employeeId)

override fun toString(): String = "Employee(employeeId=$employeeId)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.linecorp.kotlinjdsl.example.eclipselink.entity.employee

import jakarta.persistence.Column
import jakarta.persistence.Embeddable

@Embeddable
data class EmployeeAddress(
@Column(name = "zip_code")
val zipCode: String,

@Column(name = "street_address_1")
val streetAddress1: String,

@Column(name = "street_address_2")
val streetAddress2: String?,

@Column(name = "city")
val city: String,

@Column(name = "province")
val province: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@file:Suppress("JpaDataSourceORMInspection")

package com.linecorp.kotlinjdsl.example.eclipselink.entity.employee

import com.linecorp.kotlinjdsl.example.eclipselink.annotation.CompositeId
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.IdClass
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import java.io.Serializable
import java.util.*

@Entity
@Table(name = "employee_department")
@IdClass(EmployeeDepartment.EmployeeDepartmentId::class)
class EmployeeDepartment(
@Id
@Column(name = "department_id")
val departmentId: Long,
) {
@Id
@ManyToOne
@JoinColumn(name = "employee_id")
lateinit var employee: Employee

private val employeeDepartmentId get() = EmployeeDepartmentId(employee.employeeId, departmentId)

override fun equals(other: Any?): Boolean =
Objects.equals(employeeDepartmentId, (other as? EmployeeDepartment)?.employeeDepartmentId)

override fun hashCode(): Int =
Objects.hashCode(employeeDepartmentId)

override fun toString(): String =
"EmployeeDepartment(employeeDepartmentId=$employeeDepartmentId)"

@CompositeId
data class EmployeeDepartmentId(
val employee: Long,
val departmentId: Long,
) : Serializable
}
Loading

0 comments on commit b540742

Please sign in to comment.