我有一个模型联系人,该模型联系人具有与模型角色的hasMany关系。 模型角色与带有中间表RolePhone的Phone具有hasManyThrough关系。 所有检索动作均设置为急切。
检索联系人时,我会看到其“角色”列表,但每个“角色”都有一个空的“电话”列表。但是当我检索所有角色时,他们的电话列表中包含正确的电话。
在Contact和Role中,该关系均由Default标记,我尝试手动设置Role的joinDefinitions 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())
)
)
答案 0 :(得分:0)
Kazuhiro Sera在其pull request中回答,可以通过稍微改变关系来完成急切的加载
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)}")