Friday, March 13, 2009

ScalaJPA hits 1.0

I've been working on packaging up some of the Scala <-> JPA boilerplate in use for the Lift JPA Demo into a small project called ScalaJPA. After some work on documentation and bringing it up to Lift 1.0 goodness, you can find the project here:

http://scala-tools.org/mvnsites/scalajpa/

Scaladoc is here:

http://scala-tools.org/mvnsites/scalajpa/scaladocs/index.html

If you're using Maven, simply drop this dependency into your pom.xml:


<dependency>
<groupId>org.scala-libs>/groupId>
<artifactId>scalajpa>/artifactId>
<version>1.0>/version>
</dependency>


Just to give you an idea of how easy it is to use, here's how we would define a per-request EntityManager in Lift:


package com.foo.jpa

import org.scala_libs.scalajpa.{LocalEM,RequestVarEM}

object Model extends LocalEM("MyEMName") with RequestVarEM


The Model object will then act as a ScalaEntityManager that uses a new per-request EM for its operations. We can use it in our code like


...
val author = new Author("Kurt Vonnegut")
val book = new Book("Cat's Cradle", author)
Model.persistAndFlush(book)


In addition to the RequestVarEM, there are classes to handle a ThreadLocal EM as well as plain old factory classes that will let you set up and close your own ScalaEntityManagers. Check out the Lift JPA Demo site for more example code:

http://github.com/dpp/liftweb/tree/794cac5abf6b1ae5502f6321847f6186fcd8de90/sites/JPADemo

3 comments:

ifischer said...

Interesting library, thank you for contributing!
Would you say that it would also make sense to use the ScalaJPA lib in a JavaEE app (where everything is implemented with Scala)?
Or is it mainly designed to use JPA within Lift?
Are you planning to expand the library with more features?
What i'd find interesting would be some features to circumvent Scala/Java incompatibilities - for example when working with Enums.

martinwh99 said...

Thank's for the work you've done. I'm completely new to scala, jpa and java. My question is, whether there is a newer example for the scalajpa then the one you mentioned here. This one seems to use javax.persistence. I can not get my one to work. Although i can not find the RequestVarEM in my
org.scala_libs.jpa. Thanks

Derek said...

Sorry for not responding sooner. For some reason I'm not getting notified of comments! Anyways, to answer these two questions, ScalaJPA is intended to be used for any Scala code, not just Lift. In fact, the Lift-specific parts have been moved into Lift's lift-jpa module so that we have a clean split. RequestVarEM lives in lift-jpa, which is why you can't find it in org.scala_libs.jpa.

I'm hoping the in the coming few months I'll be able to work on incorporating some JPA 2.0 goodness to ScalaJPA, particularly with the query builder pieces.