Skip to content

mewebstudio/spring-boot-jpa-translatable-kotlin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Translatable for Spring Boot JPA (Kotlin)

License Maven badge javadoc

This module provides an abstract and reusable foundation for supporting translatable (multi-language) entities using Spring Data JPA.
It defines core interfaces, abstract repositories, and a base service class to handle translations with locale-specific logic.


πŸ“¦ Package Structure

com.mewebstudio.springboot.jpa.translatable
β”œβ”€β”€ ITranslatable.kt
β”œβ”€β”€ ITranslation.kt
β”œβ”€β”€ JpaTranslatableRepository.kt
β”œβ”€β”€ JpaTranslationRepository.kt
└── AbstractTranslatableService.kt

🧩 Interfaces

ITranslatable<ID, T extends ITranslation<ID, ?>>

Represents an entity that supports translations.

interface ITranslatable<ID, T : ITranslation<ID, *>> {
    val id: ID
    val translations: MutableList<T>
}

ITranslation<ID, T>

Represents a translation of an entity in a specific locale.

interface ITranslation<ID, T> {
    val id: ID
    val owner: T
    val locale: String
}

πŸ—ƒ Repositories

JpaTranslatableRepository<T : ITranslatable<ID, TR>, ID, TR : ITranslation<ID, *>> : JpaRepository<T, ID>

Generic JPA repository for translatable entities.

@NoRepositoryBean
interface JpaTranslatableRepository<T : ITranslatable<ID, TR>, ID, TR : ITranslation<ID, *>> : JpaRepository<T, ID> {
    // ...
}

JpaTranslationRepository<T, ID, OWNER>

Generic JPA repository for translation entities.

@NoRepositoryBean
interface JpaTranslationRepository<T : ITranslation<ID, OWNER>, ID, OWNER> : JpaRepository<T, ID> {
    // ...
}

🧠 Abstract Service

AbstractTranslatableService<T, ID, TR>

Provides a base service class for business logic operations.

abstract class AbstractTranslatableService<T : ITranslatable<ID, TR>, ID, TR : ITranslation<ID, *>>(
    open val repository: JpaTranslatableRepository<T, ID, TR>
) {
    // ...
}

AbstractTranslationService<T : ITranslation<ID, OWNER>, ID, OWNER>

Provides a base service class for business logic operations.

abstract class AbstractTranslationService<T : ITranslation<ID, OWNER>, ID, OWNER>(
    open val repository: JpaTranslationRepository<T, ID, OWNER>
) {
    // ...
}

πŸ“₯ Installation

for maven users

Add the following dependency to your pom.xml file:

<dependency>
  <groupId>com.mewebstudio</groupId>
  <artifactId>spring-boot-jpa-translatable-kotlin</artifactId>
  <version>0.1.1</version>
</dependency>

for gradle users

Add the following dependency to your build.gradle file:

implementation 'com.mewebstudio:spring-boot-jpa-translatable-kotlin:0.1.1'

πŸ“Œ Usage

You can extend these interfaces and abstract class to implement your own translatable entities and services:

Translatable Entity Example

@Entity
@Table(name = "categories")
class Category(
    @Id
    val id: Long,

    @OneToMany(mappedBy = "owner", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.LAZY)
    @OrderBy("locale ASC")
    override var translations: MutableList<CategoryTranslation> = mutableListOf(),
) : ITranslatable<String, CategoryTranslation> {
    override fun toString(): String = "${this::class.simpleName}(id = $id)"
}

Translation Entity Example

@Entity
class CategoryTranslation(
    @Id
    val id: Long,

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    override val owner: Category,

    @Column(name = "locale", nullable = false)
    override val locale: String,

    @Column(name = "name", nullable = false, length = 255)
    var name: String,

    @Column(name = "description", columnDefinition = "text")
    var description: String? = null,
) : ITranslation<String, Category> {
    override fun toString(): String =
        "${this::class.simpleName}(id = $id, name = $name, locale = $locale, owner = $owner)"
}

Translatable Repository Example

interface CategoryRepository : JpaTranslatableRepository<Category, String, CategoryTranslation>

Translation Repository Example

interface CategoryTranslationRepository : JpaTranslationRepository<CategoryTranslation, String, Category>

Translatable Service Example

@Service
class CategoryService(
    private val categoryRepository: CategoryRepository,
    private val categoryTranslationRepository: CategoryTranslationRepository
) : AbstractTranslatableService<Category, String, CategoryTranslation>(categoryRepository) {
    private val log: Logger by logger()

    init {
        log.debug("CategoryService initialized with repository: {}", repository)
        requireNotNull(repository) { "CategoryRepository cannot be null" }
    }

    // Custom business logic methods can be added here...

}

πŸ›  Requirements

  • Java 17+
  • Kotlin 1.9.23+
  • Spring Boot 3.x
  • Spring Data JPA

πŸ” Other Implementations

Spring Boot JPA Translatable (Java Maven Package)

πŸ’‘ Example Implementations

Spring Boot JPA Translatable - Kotlin Implementation

Spring Boot JPA Translatable - Java Implementation

πŸ“ƒ License

MIT Β© mewebstudio

About

Spring Boot JPA Translatable Kotlin - Maven package

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages