【问题标题】:Map inheritance in slick 2.0slick 2.0 中的映射继承
【发布时间】:2014-04-03 16:20:50
【问题描述】:

我知道堆栈溢出还有其他问题,但没有一个对我有用。

我正在尝试在光滑投影中映射一个简单的继承

我尝试了数百种组合,但无法编译。我以下面的代码和下面的错误结束。

我简化了案例类,因为它们有更多的数据。如果没有继承,我的其他层(控制器、服务和接口)将不得不处理复杂性,因为在这种情况下,模型表示的是三等分继承的真实对象,没有更好的表示此类继承的方法。在服务和控制器层中,我使类的 Json 表示完全符合我的需要,我可以发送和使用表示我的模型的 Json API,唯一的方法是将这种表示持久保存在关系数据库中,我的关系模型能够将这些实体保留在单个表继承中,但是将行转换为关系是很痛苦的。

我正在使用 scala 2.10.3 + sbt 0.13.1

abstract class Pessoa2(val nome:String, val tipo:String)
case class PessoaFisica2(override val nome:String, val cpf:String) extends Pessoa2(nome,"F")
case class PessoaJuridica2(override val nome:String, val cnpj:String) extends Pessoa2(nome, "J")

class PessoaTable(tag: Tag) extends Table[Pessoa2](tag, "PESSOAS"){
//    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def nome = column[String]("nome")
def tipo = column[String]("tipo")
def cpf = column[String]("CPF", O.Nullable)
def cnpj = column[String]("CNPJ", O.Nullable)

def * = (nome, tipo, cpf.?, cnpj.?) <> ({
      case (nome:String,"F",cpf:Option[String],_) => new PessoaFisica2(nome, cpf.get):Pessoa2
      case (nome:String,"J",_,cnpj:Option[String]) => new PessoaJuridica2(nome, cnpj.get):Pessoa2
    },{
    case PessoaFisica2(nome, Some(cpf)) => Some((nome, "F", cpf, ""))
    case PessoaJuridica2(nome, Some(cnpj)) => Some((nome, "J", "", cnpj))
  })
}

这以错误结束:

匿名函数的参数类型必须是完全已知的。 (SLS 8.5) [错误] 预期的类型是:? => ?

[错误] def * = (nome, tipo, cpf.?, cnpj.?) ({

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:158:类型不匹配;

发现[错误]:任意

[错误] 必需:字符串

[错误] case (nome,"F",cpf,_) => new PessoaFisica2(nome, cpf):Pessoa2

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:158:类型不匹配;

发现[错误]:任意

[错误] 必需:字符串

[错误] case (nome,"F",cpf,_) => new PessoaFisica2(nome, cpf):Pessoa2

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:159:类型不匹配;

发现[错误]:任意

[错误] 必需:字符串

[错误] case (nome,"J",_,cnpj) => new PessoaJuridica2(nome, cnpj):Pessoa2

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:159:类型不匹配;

发现[错误]:任意

[错误] 必需:字符串

[错误] case (nome,"J",_,cnpj) => new PessoaJuridica2(nome, cnpj):Pessoa2

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:160:缺少参数类型 扩展功能

[错误] 匿名函数的参数类型必须是完全已知的。 (SLS 8.5)

[错误] 预期的类型是:? => 选项[?]

[错误] },{

[错误] ^

[错误] /Users/giovanni/Projetos/atende/clientes/app/model/Pessoas.scala:157:找不到匹配的形状。

[错误] Slick 不知道如何映射给定的类型。

[错误] 可能的原因:Table[T] 中的 T 与您的 * 投影不匹配。或者您在查询中使用了不受支持的类型(例如 scala 列表)。

[错误] 所需级别:scala.slick.lifted.ShapeLevel.Flat

[错误] 源类型:(scala.slick.lifted.Column[String], scala.slick.lifted.Column[String], scala.slick.lifted.Column[Option[String]], scala.slick.提升.Column[Option[String]])

[错误] 解压类型:(String, String, String, String)

[错误] 打包类型:任意

[错误] def * = (nome, tipo, cpf.?, cnpj.?) ({

[错误] ^

[错误] 发现 7 个错误

【问题讨论】:

  • 我几乎使用 Hibernate,承担了这样做的所有成本,或者寻找替代方案。

标签: scala slick


【解决方案1】:

试试看:

abstract class Pessoa2(val nome:String, val tipo:String)
case class PessoaFisica2(override val nome:String, val cpf:String) extends Pessoa2(nome,"F")
case class PessoaJuridica2(override val nome:String, val cnpj:String) extends Pessoa2(nome, "J")


class PessoaTable(tag: Tag) extends Table[Pessoa2](tag, "PESSOAS"){
//    def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def nome = column[String]("nome")
def tipo = column[String]("tipo")
def cpf = column[String]("CPF", O.Nullable)
def cnpj = column[String]("CNPJ", O.Nullable)

def * = (nome, tipo, cpf.?, cnpj.?) <> ({ t : (String, String, Option[String], Option[String])=> t match{
    case (nome:String,"F",cpf:Option[String],_) => new PessoaFisica2(nome, cpf.get):Pessoa2
    case (nome:String,"J",_,cnpj:Option[String]) => new PessoaJuridica2(nome, cnpj.get):Pessoa2
  }},{ k: Pessoa2 => k match{
    case PessoaFisica2(nome, cpf) => Some((nome, "F", Some(cpf), Some(""))): Option[(String, String, Option[String], Option[String])]
    case PessoaJuridica2(nome, cnpj) => Some((nome, "J", Some(""), Some(cnpj))): Option[(String, String, Option[String], Option[String])]
  }})
}

应该这样编译

【讨论】:

  • 没有。错误:无法将构造函数实例化为预期类型; [错误] 发现:Some[A] [error] required: String Them a add Some(cpf:String) Some(cnpj:String) 并得到另一个类型不匹配:发现:scala.slick.lifted.Column[String] [错误] 必需:字符串
  • 您是否正在导入类似于 scala.slick.driver.MySQLDriver.simple._ 的内容?如果不尝试添加该导入。
  • 我创建了一个关于如何查询此表的后续问题here
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2011-11-22
  • 2010-11-25
相关资源
最近更新 更多