案子
本案例使用 Scala、Play Framework、Jerkson 和 Squeryl。我正在尝试将查询结果集转换为 JSON,但结果仅显示“持久”字段。
问题
为什么 Json.generate() 不打印所有实体字段?
控制器
package controllers.api
import play.api.mvc._
import play.Logger
import play.api.data.Form
import play.api.data.Forms.{mapping, text, optional}
import com.codahale.jerkson.Json
import org.squeryl.PrimitiveTypeMode._
import models.{ApplicationDatabase, Category}
object Categories extends Controller {
def findAll = Action {
val json = inTransaction {
val list = from(ApplicationDatabase.categories)(categories =>
select(categories)
orderBy(categories.title)
)
Logger.info(list.toString)
Json.generate(list)
}
Ok(json).as(JSON)
}
def findById(id: Long) = Action {
val category = inTransaction {
ApplicationDatabase.categories.lookup(id)
}
Ok(Json.generate(category)).as(JSON)
}
}
类别实体
package models
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.annotations.Column
class Category(var uid: String, var title: String) extends FlashcardsDbObject {
}
基础实体
package models;
import java.sql.Timestamp
import org.squeryl._
import org.squeryl.annotations.{Column}
import org.squeryl.PrimitiveTypeMode._
class FlashcardsDbObject extends KeyedEntity[Long] {
val id: Long = 0
@Column("created_at")
var createdAt = new Timestamp(System.currentTimeMillis)
@Column("updated_at")
var updatedAt = new Timestamp(System.currentTimeMillis)
}
问题
结果
{
persisted: true
},
{
persisted: true
},
预期的
{
id: 1,
uid: 'chemistry',
title: 'Chemistry'
persisted: true
},
{
id: 2,
uid: 'biology',
title: 'Biology'
persisted: true
},