Play2 and Anorm, how do I make the One in a One to Many relationship aware of it's Manys

孤者浪人 提交于 2019-12-08 02:21:49

问题


I have a one to many relationship between User and LinkedAccount, a User can have several linked accounts.

I have no problem in loading a LinkedAccount and it's User by doing: LinkedAccount(id, provider_user_id, salt, provider_id, auth_method, avatar_url, User.findBy(user)) in the parser.

What I can't figure out is how load a user with it's LinkedAccounts. I guess I need to make the user aware of the LinkedAccounts.. but how?

I would like to this to get rid of one extra sql call to the db everytime I want to find if the user have a linked account of the given type. Currently I do like this:

def findLinkedAccountByUserAndProvider(userId: Pk[Long], providerId : String) = {
    DB.withConnection {
      implicit connection =>
        SQL("select * from linked_account la where la.user_id = {userId} and la.provider_id = {providerId}")
          .on("userId" -> userId, "providerId" -> providerId).as(LinkedAccount.simple.singleOpt)
    }
  }

Or will this cause a problem when User is aware of it's LinkedAccounts and LinkedAccount is aware of it's user?

User:

case class User(id: Pk[Long] = NotAssigned,
                firstName: String,
                lastName: String,
                email: String,
                emailValidated: Boolean,
                lastLogin: DateTime,
                created: DateTime,
                modified: DateTime,
                active: Boolean)

object User {
  val simple = {
    get[Pk[Long]]("id") ~
      get[String]("first_name") ~
      get[String]("last_name") ~
      get[String]("email") ~
      get[Boolean]("email_validated") ~
      get[DateTime]("last_login") ~
      get[DateTime]("created") ~
      get[DateTime]("modified") ~
      get[Boolean]("active") map {
      case id ~ first_name ~ last_name ~ email ~ email_validated ~ last_login ~ created ~ modified ~ active =>
        User(id, first_name, last_name, email, email_validated, last_login, created, modified, active)
    }
  }
}

LinkedAccount:

case class LinkedAccount(id: Pk[Long] = NotAssigned,
                         providerUserId: String,
                         salt: Option[String],
                         providerId: String,
                         authMethod: Option[String],
                         avatarUrl: Option[String],
                         user: User
                         )

object LinkedAccount {
  val simple = {
    get[Pk[Long]]("id") ~
    get[String]("provider_user_id") ~
    get[Option[String]]("salt") ~
    get[String]("provider_id") ~
    get[Option[String]]("auth_method") ~
    get[Option[String]]("avatar_url") ~
    get[Pk[Long]]("user_id") map {
      case id ~ provider_user_id ~ salt ~ provider_id ~ auth_method ~ avatar_url ~ user  =>
        LinkedAccount(id, provider_user_id, salt, provider_id, auth_method, avatar_url, User.findBy(user))
    }
  }
}

回答1:


Since I got no answer I will provide my own:

In the case class I define:

case class User(id: Pk[Long] = NotAssigned,
                firstName: String,
                lastName: String,
                email: String,
                emailValidated: Boolean,
                lastLogin: DateTime,
                created: DateTime,
                modified: DateTime,
                active: Boolean) {

lazy val linkedAccounts: Seq[LinkedAccount] = DB.withConnection("test") { implicit connection =>
    SQL(
      """
        select * from linked_account la
        join users on la.user_id = users.id
        where la.id = {id}
      """
    ).on(
      'id -> id
    ).as(LinkedAccount.simple *)
  }
}

And then get the accounts by:

val linkedAccounts = user.linkedAccounts

Found this here



来源:https://stackoverflow.com/questions/15427469/play2-and-anorm-how-do-i-make-the-one-in-a-one-to-many-relationship-aware-of-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!