简体   繁体   English

Slick中嵌套类的隐式GetResult

[英]Implicit GetResult for nested classes in Slick

From what I've read, there is a way to work with nested classes to solve the problem of tables with more than 22 fields. 根据我的阅读,有一种方法可以使用嵌套类来解决超过22个字段的表的问题。 It looks like this (with a simple table): 它看起来像这样(带有一个简单的表):

case class UserRow(id:Int, address1:Address, address2:Address)
case class Address(street:String,city:String)

class User(tag:Tag) extends Table[UserRow](tag, "User"){
  def id = column[Int]("id", O.PrimaryKey)
  def street1 = column[String]("STREET1")
  def city1 = column[String]("CITY1")
  def street2 = column[String]("STREET2")
  def city2 = column[String]("CITY2")
  def * = (id, address1, address2) <> (UserRow.tupled, UserRow.unapply)
  def address1 = (street1, city1) <> (Address.tupled, Address.unapply)
  def address2 = (street2, city2) <> (Address.tupled, Address.unapply)
}

What I've realized is that plain SQL - which requires implicit values - doesn't work with this solution or at least I haven't been able to make it works. 我所意识到的是,普通的SQL - 它需要隐式值 - 不适用于这个解决方案,或者至少我无法使其工作。

I thought I could define the implicit values in the same way as the nested classes, like this: 我以为我可以像嵌套类一样定义隐式值,如下所示:

implicit val getAddressResult = GetResult(r => Address(r.<<, r.<<))
implicit val getUserResult = GetResult(r => UserRow(r.<<, r.<<, r.<<))

But it doesn't work. 但它不起作用。 It compiles but when running, it says that the user table is not found. 它编译,但在运行时,它表示找不到用户表。

I'm very new in Scala and Slick so I could have misunderstood some information or have some wrong concepts. 我在Scala和Slick中很新,所以我可能误解了一些信息或者有一些错误的概念。 What am I doing wrong? 我究竟做错了什么?

UPDATE UPDATE

This is what I'm doing in the test: 这就是我在测试中所做的事情:

user.ddl.create
user += UserRow(0, Address("s11", "c11"), Address("s12", "c12"))
user += UserRow(1, Address("s21", "c21"), Address("s22", "c22"))
user += UserRow(2, Address("s31", "c31"), Address("s32", "c32"))
println(user.list)
val sqlPlain = sql"SELECT * FROM user".as[UserRow]
println(sqlPlain)
println(sqlPlain.list)

All of it works until the last sentence where I get the error "Table "USER" not found". 所有这一切都有效,直到我收到错误“Table”USER“not found”的最后一句话。 Also the exact same test works perfect for a non nested case class. 同样的测试也适用于非嵌套的案例类。

UPDATE 2 更新2

As cvogt has correctly indicated to me, I was misunderstanding the error reported and it wasn't related to the implicit GetResult values. 正如cvogt正确地告诉我的那样,我误解了报告的错误,并且它与隐式的GetResult值无关。 His answer is correct as well as my first approach. 他的答案是正确的,也是我的第一种方法。

Pass the PositionedResult r to the corresponding GetResult objects: 将PositionedResult r传递给相应的GetResult对象:

implicit val getAddressResult = GetResult(r => Address(r.<<, r.<<))
implicit val getUserResult =
  GetResult(r => UserRow(r.<<, getAddressResult(r), getAddressResult(r)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM