在scala中使用anorm时出现运行时错误

时间:2016-02-16 10:19:52

标签: scala playframework playframework-2.0 anorm

我尝试使用anorm从数据库加载一些数据。但是我遇到了运行时错误

  

java.lang.RuntimeException:ColumnName(locations.Ref,Some(Ref))

这是我的Model类

package models
import play.api.db._
import play.api.Play.current

import anorm._
import anorm.SqlParser._

import scala.language.postfixOps
import scala.collection.mutable.ListBuffer

    case class Location(id: Int, name: String,ref:String,isactive:Int)


        object Location {

            /**
            * Parse a Location from a ResultSet
            */
            val loc = {
                get[Int]("Locations.Id") ~
                get[String]("Locations.Name")~
                get[String]("Locations.Ref")~
                get[Int]("Locations.Active") map {
                case id~name~ref~isactive => Location(id, name,ref,isactive)
                }
            }


             //Get All Locations from DB
              def findAllLocations():List[Location] = DB.withConnection { implicit c =>
                   SQL("SELECT Id,Name,Ref,Active from Locations").as(Location.loc *)
              }


}

这是我的表

enter image description here

1 个答案:

答案 0 :(得分:1)

我找到了解决方案

我已更改代码,如下所示替换get[String]("Locations.Ref")

get[Option[String]]("Locations.Ref")
val loc = {
   get[Int]("Locations.Id") ~
   get[String]("Locations.Name")~
   get[Option[String]]("Locations.Ref")~
   get[Int]("Locations.Active") map {
   case id~name~ref~isactive => Location(id, name,ref,isactive)
   }
}

并将案例类更改为ref:Stringref:Option[String]

 case class Location(id: Int, name: String,ref:Option[String],isactive:Int)