PgComposite
Marks a data class as a data type that can be mapped to a composite type in PostgreSQL database.
This annotation is crucial for TypeRegistry, which scans the classpath for marked classes to automatically build mapping between Kotlin classes and PostgreSQL composite types.
Naming convention: By default, the type name in PostgreSQL is derived from the simple class name by converting from CamelCase to snake_case (e.g., TestPerson class will be mapped to test_person type).
Explicit name specification: You can override the default name by providing it in the name parameter. This is useful when the type name in the database doesn't match the convention.
Non-reflective mapping: By default, Octavius uses reflection to map data class properties to composite attributes. You can provide a custom PgCompositeMapper via the mapper parameter to bypass reflection, which can significantly improve performance for high-volume operations.
Parameters
Optional, explicit name of the corresponding type in PostgreSQL database. If left empty, the name will be generated automatically according to the CamelCase ->snake_case convention.
Optional, custom mapper implementation to use instead of reflection. Must implement PgCompositeMapper.
Examples
// Example 1: Using default naming convention and reflection
@PgComposite
data class UserInfo(val id: Int, val username: String)
// Example 2: Explicit type name and custom mapper
@PgComposite(name = "stats_type", mapper = StatsMapper::class)
data class Stats(val strength: Int, val agility: Int)
object StatsMapper : PgCompositeMapper<Stats> {
override fun fromMap(map: Map<String, Any?>) = Stats(
strength = map["strength"] as Int,
agility = map["agility"] as Int
)
override fun toMap(obj: Stats) = mapOf(
"strength" to obj.strength,
"agility" to obj.agility
)
}