Enhancement 94 : ENHANCEMENT: basic use with Groovy and Scala
Priority 
High
Reported Version 
 
Logged By 
Rob
Status 
Fixed
Fixed Version 
1.1.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
25/03/2009
Updated 
25/03/2009
Type 
Enhancement
 
Attachments 
No attachments

That is, you should be able to use Ebean with Groovy beans and Scala beans.

Note that Scala properties have different property naming conventions.

 
Rob 25 Mar 11:51
Groovy example:
//GROOVY BEAN
package test

import javax.persistence.*;

@Entity
@Table(name="f_forum")
public class PersonG{
        
        @Id 
        Integer id
        
        @Column(name="title")
        String name
        
        @OneToMany(cascade=CascadeType.ALL)
        List<Topic> topics;
}

Rob 25 Mar 11:52
Groovy application using Ebean
//GROOVY APPLICATION using Ebean
package test

import com.avaje.ebean.*

public class MainEbean{

        public static void main(String[] args) {
                        
                List<PersonG> list = Ebean
                        .find(PersonG.class)
                        .setAutoFetch(false)
                        .join("topics")
                        .findList()
                
                println "Got list "+list
                
                list.each() { 
                        print " ${it.id} ${it.name} \n" 
                        print " GOT DETAILS: "+it.topics
                }; 
                println "done";

                println "Hello"
        }
        
        
}

Rob 25 Mar 11:53
So..

Yes, those who want to play with Groovy and Ebean can have a go.

This code is in HEAD now.

Rob 25 Mar 11:55
Scala Example
//SCALA bean ... and object/singleton
package org.stest

import javax.persistence._
import scala.reflect._
import com.avaje.ebean.annotation._
import com.avaje.ebean._

@Entity
@Table{val name="f_forum"}
class Person {

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

object Person {
  var s = Ebean.getServer(null);
  def find(id:Int):Person = {
    return s.find(classOf[Person], id)
  }
}

Rob 25 Mar 11:58
Scala Application fetching person by id
//SCALA application using Ebean 
// ... excuse my scala as I'm a complete beginner :) 
package org.stest

import com.avaje.ebean._

object TestMain extends Application {
    
    // find person 1
    var p = Person.find(1);

    println("got "+p)
    
    var t = p.topics
    var sz = t.size
    
  println("done")
}

Rob 25 Mar 12:00
Scala Example

So the support for Scala beans is in HEAD too.

NB: There is a scala bug that has just been fixed ... where the generic parameter type of a List/Set/Map is not compiled into the .class. This is why the scala Person var topics has to currently have an explicit val targetEntity = classOf[Topic].

Enjoy playing with Scala!!!

Rob 25 Mar 12:02
FUTURE:

I think there is probably a lot of things that could be done to for example give Ebean a more Scala like API ... or make use of Groovy features...

So this is the beginning and if you have ideas/thoughts I'm keen to hear them.

Thanks, Rob.

Rob 06 Apr 01:37
Better Scala DAO

This example has a better generic DAO

http://www.avaje.org/topic-137.html

woResponse

Upload a file