add common-spring-jpa
This commit is contained in:
parent
6ca9ebef8b
commit
dfd52da0d2
|
|
@ -32,3 +32,9 @@
|
|||
## common-spring
|
||||
|
||||
Набор утилит, структур данных для `spring`, без привязки к доп. модулям, таким как `jpa` и `web`
|
||||
|
||||
## common-spring-jpa
|
||||
|
||||
* `models.*` - базовые `Entity`
|
||||
* `repositories` - утилиты и доп. интерфейсы для репозиториев
|
||||
* `EnableJpaAuditingExtra` - подключение `JpaAuditing` с поддержкой типа `ZoneDateTime`
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
plugins {
|
||||
id("kotlin")
|
||||
id("kotlin-spring")
|
||||
id("maven-publish")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package ru.touchin.common.spring.jpa
|
||||
|
||||
import org.springframework.context.annotation.Import
|
||||
import ru.touchin.common.spring.jpa.configurations.AuditingConfig
|
||||
|
||||
@Suppress("unused")
|
||||
@Import(value = [AuditingConfig::class])
|
||||
annotation class EnableJpaAuditingExtra
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
@file:Suppress("SpringFacetCodeInspection")
|
||||
package ru.touchin.common.spring.jpa.configurations
|
||||
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.data.auditing.DateTimeProvider
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
|
||||
import java.time.ZoneId
|
||||
import java.time.ZoneOffset
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.Optional
|
||||
|
||||
@Configuration
|
||||
@EnableJpaAuditing(
|
||||
dateTimeProviderRef = "auditingDateTimeProvider",
|
||||
modifyOnCreate = false,
|
||||
)
|
||||
class AuditingConfig {
|
||||
|
||||
@Bean
|
||||
fun auditingDateTimeProvider(): DateTimeProvider {
|
||||
return DateTimeProvider { Optional.of(ZonedDateTime.now(ZoneId.from(ZoneOffset.UTC))) }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import org.springframework.data.annotation.CreatedBy
|
||||
import org.springframework.data.annotation.CreatedDate
|
||||
import org.springframework.data.annotation.LastModifiedBy
|
||||
import org.springframework.data.annotation.LastModifiedDate
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener
|
||||
import java.io.Serializable
|
||||
import java.time.ZonedDateTime
|
||||
import javax.persistence.EntityListeners
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@EntityListeners(AuditingEntityListener::class)
|
||||
@MappedSuperclass
|
||||
abstract class AuditableEntity : Serializable {
|
||||
|
||||
@CreatedDate
|
||||
lateinit var createdAt: ZonedDateTime
|
||||
|
||||
@CreatedBy
|
||||
lateinit var createdBy: String
|
||||
|
||||
@LastModifiedDate
|
||||
var updatedAt: ZonedDateTime? = null
|
||||
|
||||
@LastModifiedBy
|
||||
var updatedBy: String? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@Suppress("unused")
|
||||
@MappedSuperclass
|
||||
abstract class AuditableLongIdEntity : AuditableEntity() {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
open var id: Long? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator
|
||||
import java.util.UUID
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@Suppress("unused")
|
||||
@MappedSuperclass
|
||||
abstract class AuditableUuidIdEntity : AuditableEntity() {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "uuid")
|
||||
@GenericGenerator(name = "uuid", strategy = "uuid2")
|
||||
open var id: UUID? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import org.springframework.data.annotation.CreatedDate
|
||||
import org.springframework.data.annotation.LastModifiedDate
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener
|
||||
import java.io.Serializable
|
||||
import java.time.ZonedDateTime
|
||||
import javax.persistence.EntityListeners
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@EntityListeners(AuditingEntityListener::class)
|
||||
@MappedSuperclass
|
||||
abstract class BaseEntity : Serializable {
|
||||
|
||||
@CreatedDate
|
||||
lateinit var createdAt: ZonedDateTime
|
||||
|
||||
@LastModifiedDate
|
||||
var updatedAt: ZonedDateTime? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.GenerationType
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@Suppress("unused")
|
||||
@MappedSuperclass
|
||||
abstract class BaseLongIdEntity : BaseEntity() {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
open var id: Long? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.models
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator
|
||||
import java.util.UUID
|
||||
import javax.persistence.GeneratedValue
|
||||
import javax.persistence.Id
|
||||
import javax.persistence.MappedSuperclass
|
||||
|
||||
@Suppress("unused")
|
||||
@MappedSuperclass
|
||||
abstract class BaseUuidIdEntity : BaseEntity() {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(generator = "uuid")
|
||||
@GenericGenerator(name = "uuid", strategy = "uuid2")
|
||||
open var id: UUID? = null
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
@file:Suppress("unused")
|
||||
package ru.touchin.common.spring.jpa.repositories
|
||||
|
||||
object RepositoryUtils {
|
||||
|
||||
const val SKIP_LOCKED_PARAMETER = "javax.persistence.lock.timeout"
|
||||
|
||||
/**
|
||||
* Const value which is used for 'SKIP LOCKED' in 'SELECT ... FOR UPDATE' query
|
||||
*/
|
||||
const val SKIP_LOCKED_VALUE = "-2"
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -22,3 +22,4 @@ pluginManagement {
|
|||
|
||||
include("common")
|
||||
include("common-spring")
|
||||
include("common-spring-jpa")
|
||||
|
|
|
|||
Loading…
Reference in New Issue