[英]Cannot retrieve nested object list within nested object in model with SkinnyORM
我有一個具有hasMany關系的模型聯系人,它與模型角色有關。 模型角色與帶有中間表RolePhone的Phone具有hasManyThrough關系。 所有檢索動作均設置為急切。
檢索聯系人時,我會看到其“角色”列表,但每個“角色”都有一個空的“電話”列表。 但是,當我檢索所有角色時,其電話列表中包含正確的電話。
該關系在Contact和Role中都由Default標記,並且我嘗試手動設置Role的joinDefinitions enabledEvenIfAssociated = true
但這些都enabledEvenIfAssociated = true
這些是我的模型,僅顯示結構和關系
case class Contact(id: Long, name: String, roles: Seq[Role] = Nil)
object Contact extends SkinnyCRUDMapper[Contact] {
hasMany[Role](
Role -> Role.defaultAlias,
(contact, role) => sqls.eq(contact.id, role.contactId),
(contact, roles) => contact.copy(roles = roles),
).byDefault
}
case class RoleCommand(name: String, phoneNumbers: Seq[Int])
case class Role(id: Long, name: String, contactId: Long, phones: Seq[Phone] = Nil)
object Role extends SkinnyCRUDMapper[Role] {
hasManyThrough[RolePhone, Phone](
through = RolePhone -> RolePhone.defaultAlias,
throughOn = (r, rp) => sqls.eq(r.id, rp.roleId),
many = Phone -> Phone.defaultAlias,
on = (rp, p) => sqls.eq(rp.phoneId, p.id),
merge = (role, phones) => role.copy(phones = phones)
).byDefault
}
case class RolePhone(roleId: Long, phoneId: Long)
case class Phone(id: Long, number: Int)
這是我的數據庫結構
create table contact (
id bigint(20) not null auto_increment primary key,
name varchar(50) not null unique
) engine=innoDB;
create table role (
id bigint(20) not null auto_increment primary key,
name varchar(50) not null unique,
contact_id bigint(20) not null
) engine=innoDB;
create table phone (
id bigint(20) not null auto_increment primary key,
number int(20) not null
) engine=innoDB;
create table role_phone (
role_id bigint(20) not null,
phone_id bigint(20) not null
) engine=innoDB;
這是添加的Main類以暴露問題
object Main extends App with Connection with DatasourceConfig {
val contact1 = Contact.save("contact1",
Seq(
RoleCommand("role1", Seq(12345, 12321)),
RoleCommand("role2", Seq(54321, 12321)),
RoleCommand("role3", Seq(12345, 54321)),
)
)
// retrieving a contact won't fill the phones number inside each role
println(s"My Contact: ${Contact.findById(contact1)}")
// retrieving a role will fill the phones list
println(s"All Roles: ${Role.findAll()}")
}
完整代碼在https://github.com/airabinovich/testing_skinny
我希望檢索一個聯系人會得到每個角色內的電話列表。按照上面的示例,應該像這樣
Contact(
1,
contact1,
Vector(
Role(1,role1,1,Vector(Phone(1,12345), Phone(2,12321))),
Role(2,role2,1,Vector(Phone(3,54321), Phone(2,12321))),
Role(3,role3,1,Vector(Phone(1,12345), Phone(3,54321)))
)
)
但是我明白了
Contact(
1,
contact1,
Vector(
Role(1,role1,1,List()),
Role(1,role1,1,List()),
Role(2,role2,1,List()),
Role(2,role2,1,List()),
Role(3,role3,1,List()),
Role(3,role3,1,List())
)
)
正如Kazuhiro Sera在他的拉取請求中回答的那樣,可以通過稍微改變關系來完成急切的加載。
object Contact extends SkinnyCRUDMapper[Contact] {
val rolesRef = hasMany[Role](
Role -> Role.defaultAlias,
(contact, role) => sqls.eq(contact.id, role.contactId),
(contact, roles) => contact.copy(roles = roles),
).includes[Role](
merge = (contacts, roles) => contacts.map(c => c.copy(roles = roles.filter(_.contactId == c.id)))
).byDefault
}
並且從數據庫檢索時包括該關系
println(s"My Contact: ${Contact.includes(Contact.rolesRef).findById(contact1)}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.