gpt4 book ai didi

scala - 如何在 Play 2.2 中使用 ebean 框架和 scala 创建模型实例

转载 作者:行者123 更新时间:2023-12-01 23:23:47 25 4
gpt4 key购买 nike

我想用 scala 和 fremework Play 2.2 实例化 Ebean 项目的模型对象。我面临 ID 自动生成和类参数/抽象的问题:

   @Entity 
class Task(@Required val label:String) extends Model{
@Id
val id: Long
}

object Task {
var find: Model.Finder[Long, Task] = new Model.Finder[Long, Task](classOf[Long], classOf[Task])

def all(): List[Task] = find.all.asScala.toList

def create(label: String) {
val task = new Task(label)
task.save
}

def delete(id: Long) {
find.ref(id).delete
}
}

错误:“类 Task 需要是抽象的,因为未定义值 id”。有什么办法可以避免这个问题吗?

最佳答案

感谢此链接,我找到了解决方案:http://www.avaje.org/topic-137.html

import javax.persistence._
import play.db.ebean._
import play.data.validation.Constraints._
import scala.collection.JavaConverters._

@Entity
@Table( name="Task" )
class Task{

@Id
var id:Int = 0

@Column(name="title")
var label:String = null

}

/**
* Task Data Access Object.
*/
object Task extends Dao(classOf[Task]){

def all(): List[Task] = Task.find.findList().asScala.toList

def create(label: String) {
var task = new Task
task.label = label
Task.save(task)
}

def delete(id: Long) {
Task.delete(id)
}
}

还有 DAO:

/** 
* Dao for a given Entity bean type.
*/
abstract class Dao[T](cls:Class[T]) {

/**
* Find by Id.
*/
def find(id:Any):T = {
return Ebean.find(cls, id)
}

/**
* Find with expressions and joins etc.
*/
def find():com.avaje.ebean.Query[T] = {
return Ebean.find(cls)
}

/**
* Return a reference.
*/
def ref(id:Any):T = {
return Ebean.getReference(cls, id)
}


/**
* Save (insert or update).
*/
def save(o:Any):Unit = {
Ebean.save(o);
}

/**
* Delete.
*/
def delete(o:Any):Unit = {
Ebean.delete(o);
}

关于scala - 如何在 Play 2.2 中使用 ebean 框架和 scala 创建模型实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21714114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com