Skip to content

Commit

Permalink
Make sure to attach the ColumnName annotation to the field
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Aug 4, 2023
1 parent b08d685 commit 7b5c539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ Let's thus create a `ReviewWithCategory` class:
class ReviewWithCategory : Serializable {
@Nested
var review: Review = Review()
@ColumnName("name")
@field:ColumnName("name")
var categoryName: String? = null
}
```
Expand Down Expand Up @@ -450,7 +450,7 @@ The only thing that matters is that the class will have properties named exactly
using the `@ColumnName` annotation):

```kotlin
data class Beverage(@ColumnName("beverageName") var name: String = "", @ColumnName("name") var category: String? = null) : Serializable {
data class Beverage(@field:ColumnName("beverageName") var name: String = "", @field:ColumnName("name") var category: String? = null) : Serializable {
companion object {
fun findAll(): List<Beverage> = db {
handle.createQuery("select r.beverageName, c.name from Review r left join Category c on r.category = c.id")
Expand Down Expand Up @@ -689,8 +689,8 @@ Therefore, instead of database-based aliases it's better to use the `@ColumnName
such as `Customer` and projection-only entities such as `CustomerAddress`:

```kotlin
data class Customer(@ColumnName("CUSTOMER_NAME") var name: String? = null) : KEntity<Long>
data class CustomerAddress(@ColumnName("CUSTOMER_NAME") var customerName: String? = null)
data class Customer(@field:ColumnName("CUSTOMER_NAME") var name: String? = null) : KEntity<Long>
data class CustomerAddress(@field:ColumnName("CUSTOMER_NAME") var customerName: String? = null)
```

The `@ColumnName` annotation is honored both by `Dao`s and by all data loaders.
Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/com/github/vokorm/KEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.gitlab.mvysny.jdbiorm.Dao
import com.gitlab.mvysny.jdbiorm.EntityMeta
import com.gitlab.mvysny.jdbiorm.spi.AbstractEntity
import jakarta.validation.ConstraintViolationException
import org.jdbi.v3.core.mapper.reflect.ColumnName

/**
* Allows you to fetch rows of a database table, and adds useful utility methods [save]
Expand All @@ -19,7 +20,9 @@ import jakarta.validation.ConstraintViolationException
* Annotate the class with [com.gitlab.mvysny.jdbiorm.Table] to set SQL table name.
*
* ### Mapping columns
* Use the [org.jdbi.v3.core.mapper.reflect.ColumnName] annotation to change the name of the column.
* Use the [ColumnName] annotation to change the name of the column.
* Please make sure to attach the annotation the field, for example
* `@field:ColumnName("DateTime")`.
*
* ### Auto-generated IDs vs pre-provided IDs
* There are generally three cases for entity ID generation:
Expand Down Expand Up @@ -51,18 +54,20 @@ import jakarta.validation.ConstraintViolationException
*/
public interface KEntity<ID: Any> : AbstractEntity<ID> {
/**
* The ID primary key. You can use the [org.jdbi.v3.core.mapper.reflect.ColumnName] annotation to change the actual db column name.
* The ID primary key.
*
* You can use the [ColumnName] annotation to change
* the actual db column name - please make sure to attach the annotation the field, for example
* `@field:ColumnName("DateTime")`.
*/
public var id: ID?

/**
* Validates current entity. By default performs the java validation: just add `javax.validation`
* annotations to entity properties.
*
*
* Make sure to add the validation annotations to
* fields otherwise they will be ignored.
*
* fields otherwise they will be ignored. For example `@field:NotNull`.
*
* You can override this method to perform additional validations on the level of the entire entity.
*
Expand Down

0 comments on commit 7b5c539

Please sign in to comment.