Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created November 3, 2021 14:10
Show Gist options
  • Save nakamura-to/3cc0d3b5889cfe5dd2edf1e4f59c397c to your computer and use it in GitHub Desktop.
Save nakamura-to/3cc0d3b5889cfe5dd2edf1e4f59c397c to your computer and use it in GitHub Desktop.
Komapperでエンティティの関連づけを行うAPIの例
val d = Dept.meta
val e = Emp.meta
val query = SqlDsl.from(d).innerJoin(e){ d.id eq e.deptId }.associate(d, e)
val aggregate = db.runQuery { query }
val map: Map<Dept, Set<Emp>> = aggregate.oneToMany(d, e)
// associateを呼んだ分だけ実行結果から関連づけされたMapを取得できる
val d = Dept.meta
val e = Emp.meta
val a = Address.meta
val query = SqlDsl.from(d)
.innerJoin(e){ d.id eq e.deptId }
.innerJoin(a){ a.id eq e.addressId }
.associate(d, e)
.associate(e, a)
val aggregate = db.runQuery { query }
val map1: Map<Dept, Set<Emp>> = aggregate.oneToMany(d, e)
val map2: Map<Emp, Address?> = aggregate.oneToOne(e, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment