Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assumption that selfref table ID columns are always named "id" #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ fun buildToEntityFuncSelf(entityType: TypeElement, entity: EntityDefinition): Fu
private fun generateAssociationMappings(entity: EntityDefinition) =
entity.getAssociations(AssociationType.MANY_TO_ONE, AssociationType.ONE_TO_ONE)
.filter { assoc -> assoc.mapped }
.map {
val name = it.name
val targetName = it.target.simpleName
if (!it.nullable) {
.map { assoc ->
val name = assoc.name
val targetName = assoc.target.simpleName
if (!assoc.nullable) {
"\t$name = this.to$targetName()"
} else {
if (entity.hasSelfReferentialAssoc()) {
"\t$name = this.row[${entity.name}Table.${name}Id]?.let { nextAlias?.let { this.to$targetName(nextAlias, null) } }"
if (assoc.isSelfReferential) {
"\t$name = this.row[alias[${entity.tableName}.${assoc.defaultIdPropName()}]]?.let { nextAlias?.let { this.to$targetName(nextAlias, null) } }"
} else {
"\t$name = this[${entity.name}Table.${name}Id]?.let { this.to$targetName() }"
"\t$name = this.row[alias[${entity.tableName}.${assoc.defaultIdPropName()}]]?.let { this.to$targetName() }"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package pl.touk.krush.realreferences

import java.util.*
import javax.persistence.*

@Entity
@Table(name = "category")
data class Category(
@Id
@GeneratedValue
val id: Long? = null,
val uuid: UUID = UUID.randomUUID(),

@Column
val name: String,

@ManyToOne
@JoinColumn(name = "parent_id")
val parent: Category?,

@OneToMany(mappedBy = "parent")
Expand Down
21 changes: 21 additions & 0 deletions example/src/main/kotlin/pl/touk/krush/realreferences/Entry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pl.touk.krush.realreferences

import java.util.*
import javax.persistence.*

@Entity
@Table(name = "entry")
data class Entry(
@Id
val uuid: UUID = UUID.randomUUID(),

@Column
val name: String = "Data Entry",

@ManyToOne
val category: Category? = null,

@JoinTable(name = "related_entries")
@ManyToMany
val relatedEntries: List<Entry> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class CategoryTest: BaseDatabaseTest() {
val child2 = CategoryTable.insert(Category(name = "child2", parent = parent))

val categories =
CategoryTable.join(parentAlias, JoinType.LEFT, CategoryTable.parentId, parentAlias[CategoryTable.id])
CategoryTable.join(parentAlias, JoinType.LEFT, CategoryTable.parentUuid, parentAlias[CategoryTable.uuid])
.selectAll()
.map { it.toCategory(parentAlias) }

assertThat(categories)
.containsExactlyInAnyOrder(parent, child1, child2)

val fullCategoryMapping =
CategoryTable.join(parentAlias, JoinType.LEFT, CategoryTable.parentId, parentAlias[CategoryTable.id])
CategoryTable.join(parentAlias, JoinType.LEFT, CategoryTable.parentUuid, parentAlias[CategoryTable.uuid])
.selectAll()
.toCategoryList(parentAlias)

Expand All @@ -40,4 +40,25 @@ class CategoryTest: BaseDatabaseTest() {
}
}

@Test
fun shouldReferenceCategoriesShallowlyForSelfReferentialEntities() {
transaction {
SchemaUtils.create(CategoryTable, EntryTable, EntryRelatedEntriesTable)

val category = CategoryTable.insert(Category(name = "parent", parent = null))

val entryWithoutCategory = EntryTable.insert(Entry())
val entry = EntryTable.insert(Entry(category = category, relatedEntries = listOf(entryWithoutCategory)))

val entryMapping = EntryTable
.join(CategoryTable, JoinType.LEFT, EntryTable.categoryUuid, CategoryTable.uuid)
.join(EntryRelatedEntriesTable, JoinType.LEFT, EntryTable.uuid, EntryRelatedEntriesTable.entrySourceUuid)
.selectAll()
.toEntryList()

assertThat(entryMapping)
.containsExactlyInAnyOrder(entry, entryWithoutCategory)

}
}
}