New scala sql feature

52 views
Skip to first unread message

Sebastien Cesbron

unread,
Feb 9, 2011, 3:10:53 PM2/9/11
to play-fr...@googlegroups.com
Hi

Is it possible to have more details on how works the new scala sql feature. I succeeded to create a simple MEntity but I wonder how to do some things :
  • How do I define associations between entities like one-to-many and many-to-one ? When I fetch entities with associations, is there a way to define how to join them ?
  • How do I define a boolean attribute. I tried to do so but get an exception in my class.
Regards
Seb

Guillaume Bort

unread,
Feb 10, 2011, 5:16:32 AM2/10/11
to play-fr...@googlegroups.com
Hi,

We are still working on it and we are doing some refactoring to
simplify it and remove the Entity concept. But anyway the theory stay
the same.

> How do I define associations between entities like one-to-many and
> many-to-one ? When I fetch entities with associations, is there a way to
> define how to join them ?

It's not an ORM, so you can't do that (that why we are removing the
Entity concept that was error prone).

There is several way to achieve that, but I will show you an example
with 2 tables (User and Post) and a way to fetch a list of Post with
the corresponding author using the parser combinator API:

-----
case class User(email: String, password: String, fullname: String)
case class Post(title: String, content: String, author_id: Long)

object User extends MEntity[Long,User]
object Post extends MEntity[Long,Post]

del allPosts = sql("select * from Post p join User u on p.author_id =
u.id").as( Post ~< User )

// Print the result
allPosts.foreach {
case post ~ author => println(post + " - author: " + author)
}
-----

If I add a Comment table to the game:

-----
case class Comment(author: String, content: String, post_id: Long)
object Comment extends MEntity[Long,Comment]

def findPostWithComments(id: Long) = {
sql("select * from Post p join User u on p.author_id = u.id left
join Comment c on c.post_id = p.id where p.id = {id}")
.on("id" -> id)
.as(opt( Post ~< User ~< (Comment*) ))
}

val Some(post ~ author ~ comments) = findPostWithComments(4)
-----

About boolean values we are aware of this problem. In the final
version we will support all basic type corresponding to existing JDBC
types.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Sebastien Cesbron

unread,
Feb 10, 2011, 9:58:45 AM2/10/11
to play-fr...@googlegroups.com
Thanks for the answer Guillaume

I now better understand the sql test class.

In another post you briefly present the new sql framewok and said that you are creating a new sql layer because of some drawbacks of using JPA with the scala module. As far as I understand you said that they do not fit well together.

Is it possible to have more informations on this : what are the main problems you have when using JPA with the scala module ? What will be the main advantages of your alternate sql solution and what are its main goal. For you, do they address different use cases or is the sql alternative a better choice when working with play-scala ?

Regards
Sebastien

Sadek Drobi

unread,
Feb 10, 2011, 6:12:59 PM2/10/11
to play-fr...@googlegroups.com
I will try to distinguish two things, JPA and ORMs in general.
ORMs try to map relational data (db) to object. Let's imagine objects as rounded things (or circles) and relational databases as squares. ORMs try to fit these rectangles in circles. They obviously fail in this mapping. We encounter this failure quite often in various cases of weird behavior in advanced relations and/or having often to write HQL or SQL that bring you to think back about the way database is designed.

ORMs (including JPA) have failed clearly to provide a mapping layer that let you focus on the domain most of the time. It even forces you to bend one side into the shape of the other, either the "object" model to look like the database table or tables to look like objects. Anyway, modern persistence framework understood well this idea and don't try anymore to do a mapping but rather to mirror the database in a similar model providing some useful APIs.

There is one thing, however, that has been very important for users of jave: data transformation. Transforming data from one shape to another in java is at best painful. This is the main reason why ORM-like framework still exist and popular (together with how hard to use JDBC APIs are).

Scala on its side solves these both issues. Scala provides the tools to do data transformation trivially. It also provides the conceptual power that allows to wrap JDBC in a more convenient, user friendly API.

Some examples of available data transformation tools are functions and pattern matching, rich various collection API and parser combinators. The main idea of the SQL package is make use of this power.

That was about the whole ORM concept in general. For JPA, and forgetting about the whole concept, it doesn't handle main concepts of Scala language like Option types (that replace nulls). Of course you can still use JPA if you need, but being in era where you'll be faced at some point to be handeling data of different types/sorts, you need to practice more data transformation. 
www.sadekdrobi.com
ʎdoɹʇuǝ

Tomas Herman

unread,
Feb 10, 2011, 6:31:51 PM2/10/11
to play-framework
Hey guys,
i think this will be a bit offtopic but is there any reason
playframework scala version doesn't use some sort of scala orm
framework? Perhaps something like Squeryl?
Thanks for the answer,
Regards, Tomas Herman
> > On Wed, Feb 9, 2011 at 9:10 PM, Sebastien Cesbron <scesb...@gmail.com>
> > wrote:
> > > Hi
>
> > > Is it possible to have more details on how works the new scala sql
> > feature.
> > > I succeeded to create a simple MEntity but I wonder how to do some things
> > :
>
> > > How do I define associations between entities like one-to-many and
> > > many-to-one ? When I fetch entities with associations, is there a way to
> > > define how to join them ?
> > > How do I define a boolean attribute. I tried to do so but get an
> > exception
> > > in my class.
>
> > > Regards
> > > Seb
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "play-framework" group.
> > > To post to this group, send email to play-fr...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > play-framewor...@googlegroups.com.
> > > For more options, visit this group at
> > >http://groups.google.com/group/play-framework?hl=en.
>
> > --
> > Guillaume Bort,http://guillaume.bort.fr
>
> > For anything work-related, use g...@zenexity.fr; for everything else,
> > write guillaume.b...@gmail.com

Guillaume Bort

unread,
Feb 11, 2011, 11:42:18 AM2/11/11
to play-fr...@googlegroups.com
You can probably already use Squeryl with Play.

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Reply all
Reply to author
Forward
0 new messages