我有这些表:USERS、USER_ROLES、ROLES、PERMISSIONS、ROLE_PERMISSIONS
我正在实施:
AssignedRoles (M): returns the set of roles assigned to a given user;
所以,如果我要为这个函数编写查询,它会像这样:
SELECT role_id FROM USER_ROLES WHERE user_id = 'M'
其中 M 是给定的用户 id,然后我会通过他们的 id 查找每个角色并返回 Role 对象,或者我会使用连接,但这在这里不相关。
那么我的 UserRole 模型是什么样的:
case class UserRole(id:Long, userID:Long, roleID:Long)
object UserRoles {
class UserRoles(tag: Tag) extends Table[UserRole](tag, "USER_ROLES") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def userID = column[Long]("USER_ID")
def roleID = column[Long]("ROLE_ID")
def user = foreignKey("USER_ID", userID, TableQuery[Users])(_.id)
def role = foreignKey("ROLE_ID", roleID, TableQuery[Roles])(_.id)
def * = (id, userID, roleID) <> (UserRole.tupled, UserRole.unapply)
}
def retrieveRoles(userID:Long) : List[Role] = {
DB.withSession { implicit session =>
userRoles.filter(_.userID === userID).list
}
}
正如预期的那样,retrieveRoles 返回一个 UserRoles 列表,但我想要一个角色列表,因此我必须编写另一个查询,该查询将采用 UserRole.roleID 并在每个返回的 UserRole 的角色表中找到它。这是很多查询,我觉得有一种方法可以在一个查询中执行此操作。Slick 专家有什么帮助吗?