To create queries, you need two things: a database connection and a session, so your problem is how you define and use them. With Database.threadLocalSession
in scope, you can do your queries as follows:
Database.forURL("jdbc:h2:mem:play", driver = "org.h2.Driver") withSession { //create table Foo.ddl.create //insert data Foo.insert((1.toLong,"foo","bar")) //get data val data : (Long,String,String) = (for{f<-Foo}yield(f)).first }
or you can do it like this:
val database = Database.forDataSource(DB.getDataSource()) database.withSession{ implicit session : Session => Foo.ddl.create Foo.insert((1.toLong,"foo","bar")) val data : (Long,String,String) = (for{f<-Foo}yield(f)).first }
I created a test and it works great, you can play with it:
"Foo should be creatable " in { running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { val database = Database.forDataSource(DB.getDataSource()) database.withSession{ implicit session : Session => Foo.ddl.create Foo.insert((1.toLong,"foo","bar")) val data : (Long,String,String) = (for{f<-Foo}yield(f)).first data._1 must equalTo(1) } } }
You can also look here.
source share