播放框架表单映射递归项
Posted
技术标签:
【中文标题】播放框架表单映射递归项【英文标题】:Play framework Form mapping recursive item 【发布时间】:2014-05-05 13:37:47 【问题描述】:我有以下案例类,它的伴随对象中有一个表单映射:
case class ContractItem(description: String, price: Option[BigDecimal], durationMonths: Option[Int], subItems: List[ContractItem])
object ContractItem
val mapping: Mapping[ContractItem] = Forms.mapping(
"description" -> text,
"price" -> optional(bigDecimal),
"durationMonths" -> optional(number),
"subItems" -> Forms.list(ContractItem.mapping)
)(ContractItem.apply)(ContractItem.unapply)
正如您所见,案例类是递归的,因为它包含一个相同类型的列表,我试图将其包含在映射中。一切都可以编译,但是当我尝试运行这个项目时,我得到以下信息:
java.lang.NullPointerException
at play.api.data.RepeatedMapping.<init>(Form.scala:636)
at play.api.data.Forms$.list(Forms.scala:389)
at models.Installation$ContractItem$.<init>(Installation.scala:17)
at models.Installation$ContractItem$.<clinit>(Installation.scala)
at models.Installation$Contract$.<init>(Installation.scala:23)
at models.Installation$Contract$.<clinit>(Installation.scala)
at models.Installation$.<init>(Installation.scala:31)
at models.Installation$.<clinit>(Installation.scala)
at Temp.<init>(Temp.scala:82)
at .<init>(<console>:7)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
知道这样的事情是否可能吗?应该是我想的!或者有什么解决方法的想法?
谢谢! NFV
更新:
使用
lazy val mapping
结果如下:
java.lang.***Error
at play.api.data.Mapping$class.$init$(Form.scala:416)
at play.api.data.OptionalMapping.<init>(Form.scala:704)
at play.api.data.Forms$.optional(Forms.scala:363)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:15)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
at models.Installation$ContractItem$.mapping$lzycompute(Installation.scala:17)
at models.Installation$ContractItem$.mapping(Installation.scala:13)
【问题讨论】:
这方面有什么更新吗? @John 相关 xkcd - xkcd.com/979 【参考方案1】:变化:
val mapping: Mapping[ContractItem]
到:
lazy val mapping: Mapping[ContractItem]
通过使mapping
变得懒惰,它可以引用自己而不抛出 NPE。
【讨论】:
以上是关于播放框架表单映射递归项的主要内容,如果未能解决你的问题,请参考以下文章