2013-02-28 39 views
0

我想通过获取正确的linkedAccount来选择耦合用户。 创建的查询是正确的,但试图在dbuser上使用属性 例如dbuser.lastName我得到编译错误,因为dbuser不是 类型为User但Query1大小=?
它可能是一个非常简单的东西,但我无法弄清楚,因为我是 scala和squeryl noob!使用squeryl和scala时出现奇怪的结果

为什么它不返回正确的值,以及我在查询中做了什么错误? 另外,保存作品没有任何问题。

用户:

class User(
      @Column("id") val id: Long, 
      @Column("first_name") val firstName : String, 
      @Column("last_name") val lastName : String, 
      @Column("email") val email : String, 
      @Column("email_validated") val emailValidated: Boolean = false, 
      @Column("last_login") val lastLogin: Timestamp = null, 
      val created: Timestamp, 
      val modified: Timestamp, 
      val active: Boolean = false 
       ) extends KeyedEntity[Long] { 

    lazy val linkedAccounts: OneToMany[LinkedAccount] = AppDB.usersToLinkedAccounts.left(this) 
} 

LINKEDACCOUNT:

class LinkedAccount(
        @Column("id") val id: Long, 
        @Column("user_id") val userId: Long, 
        @Column("provider_user_id") val providerUserId: String, 
        @Column("salt") val salt: String, 
        @Column("provider_key") val providerKey: String) extends KeyedEntity[Long] { 

    lazy val user: ManyToOne[User] = AppDB.usersToLinkedAccounts.right(this) 

} 

AppDB:

object AppDB extends Schema { 
    val users = table[User]("users") 
    val linkedAccounts = table[LinkedAccount]("linked_account") 
    val usersToLinkedAccounts = oneToManyRelation(users, linkedAccounts).via((u, l) => u.id === l.userId) 

def userByLinkedAccount(prodivderKey: String, providerUserId: String) = { 
    from(AppDB.users)(u => 
     where(u.id in 
     from(AppDB.linkedAccounts)(la => 
      where(la.userId === u.id and la.providerKey === prodivderKey and la.providerUserId === providerUserId) 
      select (la.userId) 
     ) 
    ) 
     select (u) 
    )  
    } 

呼叫:

val dbuser = inTransaction { 
     val u2 = AppDB.userByLinkedAccount(id.providerId, id.id) 
     println(u2.statement)    
    } 
    println(dbuser.lastName) 

生成的SQL

Select 
    users10.last_login as users10_last_login, 
    users10.email as users10_email, 
    users10.modified as users10_modified, 
    users10.last_name as users10_last_name, 
    users10.first_name as users10_first_name, 
    users10.id as users10_id, 
    users10.created as users10_created, 
    users10.email_validated as users10_email_validated, 
    users10.active as users10_active 
From 
    users users10 
Where 
    (users10.id in ((Select 
    linked_account13.user_id as linked_account13_user_id 
    From 
    linked_account linked_account13 
    Where 
    (((linked_account13.user_id = users10.id) and (linked_account13.provider_key = 'facebook')) and (linked_account13.provider_user_id = 'XXXXXXXXXX')) 
))) 

回答

0

好了它。我需要拨打电话,在这种情况下:

.headOption 

而且固定后查询的一些技巧,从每

def userByLinkedAccount(providerKey : String, providerUserId : String) = { 
    inTransaction { 
     from(AppDB.users, AppDB.linkedAccounts)((u,la) => 
     where (u.id === la.userId and la.providerKey === providerKey and la.providerUserId === providerUserId) 
      select(u) 
    ).headOption 
    } 
    } 
+0

你可能要考虑使用'headOption'代替,而你的情况会返回一个'选项[用户]'。小心使用'single',因为如果没有数据(或者返回多行)它会引发异常。 – jcern 2013-02-28 13:14:44

+0

啊!完美的提示!啊哈! – jakob 2013-02-28 14:06:31

0

顺便说一句,在文档中@Column@ColumnBase据说:

定义列元数据的首选方式不是没有定义它们(!)

所以,你可以定义列,就像

val id: Long,

,而不是

@Column("id") val id: Long

+0

啊!感谢您的小费! – jakob 2013-02-28 11:58:29