diff --git a/annotation-processor/src/main/kotlin/pl/touk/krush/source/TablesGenerator.kt b/annotation-processor/src/main/kotlin/pl/touk/krush/source/TablesGenerator.kt index cd4a279..be6888e 100644 --- a/annotation-processor/src/main/kotlin/pl/touk/krush/source/TablesGenerator.kt +++ b/annotation-processor/src/main/kotlin/pl/touk/krush/source/TablesGenerator.kt @@ -13,6 +13,7 @@ import pl.touk.krush.meta.toClassName import pl.touk.krush.validation.* import javax.lang.model.element.Name import javax.lang.model.element.TypeElement +import javax.persistence.JoinColumn @KotlinPoetMetadataPreview class TablesGenerator : SourceGenerator { @@ -350,7 +351,10 @@ class TablesGenerator : SourceGenerator { } private fun associationInitializer(assoc: AssociationDefinition, idProp: PropertyDefinition) : CodeBlock { - val columnName = assoc.joinColumns.find { it.name == idProp.columnName.toString() }?.name + // if it's embedded key - match by id column name, otherwise - just use JoinColumn name + val joinColumnFinder: (JoinColumn) -> Boolean = + if (assoc.targetId.embedded) { x -> x.name == idProp.columnName.toString() } else { _ -> true } + val columnName = assoc.joinColumns.find(joinColumnFinder)?.name ?: "${assoc.name.asVariable()}_${assoc.targetId.name.asVariable()}" val idCodeBlock = idCodeBlock(idProp, assoc.target.simpleName, columnName) diff --git a/example/src/main/kotlin/pl/touk/krush/one2many/bidi/Tree.kt b/example/src/main/kotlin/pl/touk/krush/one2many/bidi/Tree.kt index fb299e4..87313eb 100644 --- a/example/src/main/kotlin/pl/touk/krush/one2many/bidi/Tree.kt +++ b/example/src/main/kotlin/pl/touk/krush/one2many/bidi/Tree.kt @@ -29,7 +29,7 @@ data class Branch( val name: String, @ManyToOne - @JoinColumn(name = "tree_id") + @JoinColumn(name = "my_tree_id") val tree: Tree? = null, @OneToMany(mappedBy = "branch") diff --git a/example/src/main/kotlin/pl/touk/krush/one2many/uni/Tree.kt b/example/src/main/kotlin/pl/touk/krush/one2many/uni/Tree.kt index a81f37c..0fc1ec2 100644 --- a/example/src/main/kotlin/pl/touk/krush/one2many/uni/Tree.kt +++ b/example/src/main/kotlin/pl/touk/krush/one2many/uni/Tree.kt @@ -15,7 +15,7 @@ data class Tree( val name: String, @OneToMany - @JoinColumn(name = "tree_id") + @JoinColumn(name = "my_tree_id") val branches: List = emptyList() )