Please use the google group to ask questions - thanks.

by Rob 03 Apr 11:11
Scala support - pointers, suggestions welcome

Hi,

With v1.1.0 Ebean is quite happy with Scala beans.

However, I'm pretty sure there is quite a bit to learn in terms of good approaches to using Ebean with Scala. If you have any ideas or pointers I'd be very interested to hear them.

Example of Scala and Ebean: http://www.avaje.org/bugdetail-94.html

This example http://www.hars.de/2009/03/jpa-with-scala.html shows use of "case class" for compound keys.

Cheers, Rob.

03 Apr 11:52
by Rob
03 Apr 11:53
by Rob

Example Scala Bean:

package org.stest

import javax.persistence._
import scala.reflect._
import com.avaje.ebean.annotation._
import com.avaje.ebean._
import java.util.logging._

/**
 * Person entity bean.
 */
@Entity
@Table{val name="f_forum"}
class Person {

  @Id 
  var id:Int = 0
    
  @Column{val name="title"}
  var name:String = null  
  
  @OneToMany{val targetEntity = classOf[Topic], val cascade = Array(CascadeType.ALL)}
  var topics: java.util.List[Topic] = null
}

/**
 * Person Data Access Object.
 */
object Person extends Dao(classOf[Person]){
    
}
03 Apr 11:53
by Rob

Example Scala Data Access Object:

package org.stest

import java.util.logging.Logger
import com.avaje.ebean._

/**
 * Dao for a given Entity bean type.
 */
abstract class Dao[T](cls:Class[T]) {
 
  private val ebeanServer = Ebean.getServer(null);
 
  /**
   * Find by Id.
   */
  def find(id:Any):T = {
    return ebeanServer.find(cls, id)
  }
  
  /**
   * Find with expressions and joins etc.
   */
  def find():com.avaje.ebean.Query[T] = {
    return ebeanServer.find(cls)
  }

  /**
   * Return a reference.
   */
  def ref(id:Any):T = {
    return ebeanServer.getReference(cls, id)
  }
  
  
  /**
   * Save (insert or update).
   */
  def save(o:Any):Unit = {
    ebeanServer.save(o);
  }
  
  /**
   * Delete.
   */
  def delete(o:Any):Unit = {
    ebeanServer.delete(o);
  }
}
03 Apr 11:55
by Rob

The Dao above is generic... so if you look at the Person bean, you will see the object Person extends Dao...

03 Apr 11:57
by Rob

An a Example use:

package org.stest

import com.avaje.ebean._

object TestMain extends Application {
  
  val p = Person.find(1);
  println("got "+p.id + " "+p.name)
    
  p.name = "Changed Name";
  Person.save(p);
  
  // find people with Id > 2
  val personList = Person.find().where().gt("id",2).findList();
  
  println("done")
}
03 Apr 12:07
by Rob

Note: The val ebeanServer = Ebean.getServer(null); returns the "default Ebean Server". There is one EbeanServer per DataSource.

Enjoy, Rob.

19 Oct 13:23
by Mike

Just a note, since Scala 2.8, the annotation syntax has changed. The above example won't work anymore. Here is an updated model I am using:

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

@Id
var id:Int = 0

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

Notice the Parens instead of the Brackets and there are no more val declarations needed.

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse