I'm using 1.8.0 and making use of the association ...
# komapper
z
I'm using 1.8.0 and making use of the association API. I was wondering how can I make my entity have a default value for an assocation?
Copy code
@KomapperEntity
@KomapperAggregateRoot("guilds")
@KomapperOneToOne(targetEntity = MarkovConfig::class)
@KomapperOneToMany(targetEntity = Filter::class, navigator = "filters")
data class Guild(
    @KomapperId
    val id: Long,
    val markovConfigId: Int,
    @KomapperColumn(alternateType = ClobString::class)
    val data: String = ""
)

@KomapperEntity
@KomapperOneToOne(targetEntity = Guild::class)
data class MarkovConfig(
    @KomapperId
    val id: Int = 0,
    val enabled: Boolean = true,
    val frequency: Float = 0.5f,
    val handleMention: Boolean = true
)
When creating the Guild entity, I would like to only have to specify the id, with a default MarkovConfig entity associated, is this possible? Any other advice on my code would be appreciated
t
You can use
@KomapperIgnore
to hold a default value:
Copy code
@KomapperEntity
@KomapperAggregateRoot("guilds")
@KomapperOneToOne(targetEntity = MarkovConfig::class, navigator = "realMarkovConfig")
@KomapperOneToMany(targetEntity = Filter::class, navigator = "filters")
data class Guild(
    @KomapperId
    val id: Long,
    val markovConfigId: Int,
    @KomapperColumn(alternateType = ClobString::class)
    val data: String = "",
    @KomapperIgnore
    val markovConfig: MarkovConfig = MarkovConfig()
)
When retrieving the association, you can get the real association from your own function and return the default value if you cannot get it:
Copy code
fun integration.jdbc.Guild.`markovConfig`(
    store: org.komapper.core.dsl.query.EntityStore,
    source: example._Guild = org.komapper.core.dsl.Meta.`guild`,
    target: example._MarkovConfig = org.komapper.core.dsl.Meta.`markovConfig`,
): integration.jdbc.MarkovConfig? {
    return realMarkovConfig(store, source, target) ?: this.markovConfig
}