【发布时间】: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,承担了这样做的所有成本,或者寻找替代方案。