AnjunarDesign · Software · Sound
All frameworks
ANJUNAR / OPEN SOURCE

Hibernate I18n

Typed, locale-bound entity fields backed by relational translations.

The idea

Hibernate I18n keeps translated content in separate rows while entity fields retain their ordinary types. Version 1.1.0 derives translation access from annotations and fixes a content locale per Session for Hibernate 7.4.10.Final and PostgreSQL.

What it does.

01

Ordinary entity fields

Mark String fields or converted values with @Translation; the runtime mapping stores their values per locale.

02

Locale-bound queries

Load, filter, order and page by translated values with a fixed Session locale and field-level fallback.

03

Translation editor

Read and write exact inactive locales through a typed handle, with tenant isolation and optimistic row versions.

Installation

Add to your project
libraryDependencies += "com.anjunar" % "hibernate-i18n" % "1.1.0"

Package: com.anjunar:hibernate-i18n

Start building.

Use hibernate-i18n 1.1.0 and bootstrap with HibernateI18n.registryBuilder(). Declare translations only with @Localized, @Translation and standard JPA annotations. openSession discovers the mapping automatically; no field registrations or separate codecs are needed.

Typed fields, translated rows

The entity keeps a String title. The runtime maps its values into locale-specific rows rather than a title column on the parent.

Source code
import com.anjunar.hibernatei18n.annotation.{Localized, Translation}
import jakarta.persistence.{Entity, Id, Table}
import java.util.UUID
import scala.compiletime.uninitialized

@Entity
@Table(name = "page")
@Localized(defaultLocale = "en", fallbackLocale = "en")
class Page:
  @Id
  var id: UUID = uninitialized
  @Translation
  var title: String = uninitialized

An exact-locale editor

After runtime bootstrap, get the metadata-derived handle. Edit English through it while the managed entity retains its German Session value.

Source code
import com.anjunar.hibernatei18n.runtime.HibernateI18n

val translations = HibernateI18n.translations(factory, classOf[Page])

val session = HibernateI18n.openSession(factory, "de")
try
  val tx = session.beginTransaction()
  try
    val page = session.find(classOf[Page], pageId)
    val english = translations.get[String](session, page, "title", "en")
    translations.set(session, page, "title", "en", "Hello")
    tx.commit()
  catch
    case error: Throwable =>
      if tx.isActive then tx.rollback()
      throw error
finally session.close()

By design.

One Session owns one content locale. Domain properties apply exact-locale, language, configured fallback and default-locale lookup per field; the editor reads exact overrides. The runtime rejects detached merge, replicate and query caching. Its bootstrap guard refuses incomplete localized mappings.