Created
August 16, 2012 17:25
-
-
Save mbseid/3371839 to your computer and use it in GitHub Desktop.
Using Ebean with Play! Framework 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package daos; | |
import java.util.*; | |
import javax.persistence.*; | |
import play.*; | |
import play.db.ebean.*; | |
import play.db.ebean.Model.*; | |
import play.data.format.*; | |
import play.data.validation.*; | |
import models.*; | |
import com.avaje.ebean.common.BeanList; | |
import scala.Option; | |
public class CrudDAO <T extends AbstractModel> { | |
private T t; | |
private Class<T> classType; | |
protected Finder<Long,T> find; | |
public CrudDAO(Class<T> typeParameterClass){ | |
classType = typeParameterClass; | |
find = new Finder<Long,T>( | |
Long.class, classType | |
); | |
} | |
public List<T> all(){ | |
return find | |
.where() | |
.findList(); | |
} | |
public Option<T> get(Long id){ | |
return Option(find | |
.where() | |
.eq("id", id) | |
.findUnique()); | |
} | |
public T save(T obj){ | |
if(obj.id != null){ | |
obj.update(); | |
}else{ | |
obj.save(); | |
} | |
return obj; | |
} | |
public void delete(Long id){ | |
find.byId(id).delete(); | |
} | |
protected Option<T> Option(T obj){ | |
return scala.Option.apply(obj); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db.default.driver=org.h2.Driver | |
# db.default.url="jdbc:h2:mem:play" | |
# This lets Ebean know what namespace your model is located | |
ebean.default="models.*" | |
# For debugging generated sql statements | |
db.default.logStatements=true | |
logger.com.jolbox=DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sbt._ | |
import Keys._ | |
import PlayProject._ | |
object ApplicationBuild extends Build { | |
val appName = "Blog" | |
val appVersion = "1.0-SNAPSHOT" | |
val modelDependencies = Seq( | |
"postgresql" % "postgresql" % "9.1-901-1.jdbc4" | |
) | |
val mainDependencies = Seq( | |
"com.typesafe.akka" % "akka-actor" % "2.0.1" | |
) | |
val model = PlayProject(appName + "-model", appVersion, modelDependencies, path = file("models"), mainLang = JAVA) | |
val main = PlayProject(appName, appVersion, mainDependencies, mainLang = SCALA).settings( | |
resolvers += "Novus Release Repository" at "http://repo.novus.com/releases/", | |
resolvers += "Novus Snapshots Repository" at "http://repo.novus.com/snapshots/" | |
).dependsOn(model) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait EBeanConversions{ | |
implicit def convertjavaList2SclaList[T](list:java.util.List[T]):List[T] = list.asScala.toList | |
implicit def convertScalaListToJavaList(aList:List[List[String]]) = java.util.Arrays.asList(aList.toArray: _*) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment