Cascade Mapping

Define a one-to-one relationship

Use @Cascade(properties, targetProperties) on a KPojo property to describe how the current row points to the target row. The first array uses properties on the current class; the second array uses properties on the target class.

one to one

import com.kotlinorm.annotations.Cascade
import com.kotlinorm.annotations.Table
import com.kotlinorm.interfaces.KPojo

@Table("tb_employee")
data class Employee(
    var id: Int? = null,
    var profileId: Int? = null,
    @Cascade(["profileId"], ["id"])
    var profile: EmployeeProfile? = null
) : KPojo

@Table("tb_employee_profile")
data class EmployeeProfile(
    var id: Int? = null,
    var displayName: String? = null
) : KPojo

Relationship shape:

shape

Employee.profileId -> EmployeeProfile.id

Define a one-to-many relationship

Use a collection property when one source row can load multiple target rows.

one to many

@Table("tb_company")
data class Company(
    var id: Int? = null,
    var name: String? = null,
    @Cascade(["id"], ["companyId"])
    var employees: List<Employee>? = null
) : KPojo

@Table("tb_employee")
data class Employee(
    var id: Int? = null,
    var companyId: Int? = null,
    var name: String? = null
) : KPojo

Relationship shape:

collection shape

Company.id -> Employee.companyId

Match multiple fields

Pass the same number of properties in both arrays when a relationship uses a composite key.

multi field

@Table("tb_order_line")
data class OrderLine(
    var tenantId: Int? = null,
    var orderNo: String? = null,
    var lineNo: Int? = null,
    @Cascade(["tenantId", "orderNo"], ["tenantId", "orderNo"])
    var order: OrderHeader? = null
) : KPojo

@Table("tb_order_header")
data class OrderHeader(
    var tenantId: Int? = null,
    var orderNo: String? = null,
    var customerName: String? = null
) : KPojo

Relationship shape:

multi shape

OrderLine.tenantId -> OrderHeader.tenantId
OrderLine.orderNo  -> OrderHeader.orderNo

Choose which cascade operations use the mapping

The optional usage parameter limits the operations that use this relationship mapping. Leave it unset when insert, update, delete, upsert, and select can all use the relationship.

usage

import com.kotlinorm.enums.KOperationType.SELECT

@Cascade(["id"], ["companyId"], usage = [SELECT])
var employees: List<Employee>? = null

@Cascade defines the relationship metadata. Operation pages show how to execute with that metadata: 📚Cascade Select, 📚Cascade Insert, 📚Cascade Update, and 📚Cascade Delete.

See 📚Ignore when a relationship property should be skipped by default in automatic cascade select.