automatic from(...) to JSON transformation in scalatra

29 views
Skip to first unread message

boris

unread,
Oct 18, 2014, 5:40:02 AM10/18/14
to squ...@googlegroups.com
Hi @all!

I am a scala noob doing my first steps with scalatra.

I would like to use scalatra as a backend for my webapp.
My first goal was to generate new players (hardcoded), persist them, read them from database and return them as JSON.

So first I followed the scalatra JSON guide to change the default output to JSON.
Then I followed the scalatra SQUERYL guide to get a minimum of persistence working.

Now I am stuck at the point where the selection result should be returned:

get("/") {
    inTransaction {     
      List(from(MassTournamentSchema.players)(select(_)))
    }
}

I get the following error: "No session is bound to current thread, a session must be created via Session.create and bound to the thread via 'work' or 'bindToCurrentThread' Usually this error occurs when a statement is executed outside of a transaction/inTrasaction block ".

If I iterate over the result inside the "inTransaction{...}" block, everything is okay.

Reading http://squeryl.org/selects.html says: "[...] the query is sent to the database only when starting iteration, or in other words, when Iterable.iterator is called. [...]".

Does anyone have an idea?
Is my setup for auto JSON generation not working if I try to select from database and return directly?
THX!

Joshua Backfield

unread,
Oct 18, 2014, 2:22:12 PM10/18/14
to squ...@googlegroups.com
I think your issue is that you're creating a List of Queryables, try 

get("/") {
    inTransaction {      
      from(MassTournamentSchema.players)(select(_)).toList
Reply all
Reply to author
Forward
0 new messages