【发布时间】:2014-03-02 03:37:30
【问题描述】:
我在使用 Play 开发 Web 应用程序时遇到了一些小问题!框架、Scala 和 Anorm 与 Mysql。
我有这两个案例类:
case class Supplier(id : Pk[Long]= NotAssigned,
identityId : Option[SupplierIdentity],
addressId : Option[Address], userId : User) {}
object Supplier {
val simple = {
get[Pk[Long]]("Supplier.id") ~
SupplierIdentity.simple ~
Address.simple ~
User.simple map {
case id ~ identityId ~ addressId~ userId =>
Supplier(id, Some(identityId), Some(addressId), userId)
}
和
case class Address(id : Pk[Long] = NotAssigned, via : String, cap : Int, comune : String, provincia : String, paese : String) {
}
object Address {
val simple = {
get[Pk[Long]]("Address.id") ~
get[String]("Address.via") ~
get[Int]("Address.cap") ~
get[String]("Address.comune") ~
get[String]("Address.provincia") ~
get[String]("Address.paese") map {
case id ~ via ~ cap ~ comune ~ provincia ~ paese=>
Address(id, via ,cap , comune , provincia , paese)
}
}
在尝试检索供应商时,我有这个查询来访问我的数据库:
def findByIdentity(identityId: Long) : Option[Supplier]= {
DB.withConnection {implicit connetion =>
SQL("""SELECT *
FROM Supplier
LEFT JOIN SupplierIdentity
ON Supplier.identity_id= SupplierIdentity.id
LEFT OUTER JOIN Address
ON Supplier.address_id= Address.id
LEFT JOIN User
ON Supplier.user_id= User.id
WHERE Supplier.identity_id={id}""").on('id->identityId).as(Supplier.simple.singleOpt)
}
}
而且我确信它可以工作,因为我已经在 Mysql 服务器上尝试过,它可以使用正确的值:
id | identity_id | address_id | user_id | id | supplier_code | type | ragione_sociale | partita_iva | is_production | id | via | cap | comune | provincia | paese | id | name |
+----+-------------+------------+---------+------+---------------+------+-----------------+-------------+---------------+------+------+------+--------+-----------+-------+------+---------+
| 2 | 2 | NULL | 1 | 2 | 1 | q | q | 1 | 1 | NULL | NULL | NULL | NULL | NULL | NULL | 1 | Tiziano |
但是当我尝试从我的控制器中使用此方法时,我收到此错误:
[运行时异常: UnexpectedNullableFound(ColumnName(Address.id,Some(id)))]
这是我的问题,因为这些列可能为空,但我找不到处理空值的方法。我希望 wuery 能够正常工作,即使它的某些列为空。 你能给我一些建议吗?
【问题讨论】:
标签: mysql scala playframework-2.0