문제

I would like to instance a model object of the Ebean project with scala and the fremework Play 2.2. I face to an issue with the ID autogenerate and the class parameteres / abstraction :

   @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
  }
}

The error : "class Task needs to be abstract, since value id is not defined". Any idea to avoid this problem?

도움이 되었습니까?

해결책

I found the solution thanks this link : 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)
  }
}

And the 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);  
  }  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top