Scala with JPA

February 3, 2009

In my quest to make Scala blend with JSF and JPA, I’ve been trying to get an app working where I interact with the database while using Scala entities. Obviously, as a first step, I got a simple JPA project working, where my User class looked like:

import javax.persistence.*;

@Table(name = "user")
@NamedQueries({@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE = :id"), 
@NamedQuery(name = "User.findByFirstname", query = "SELECT u FROM User u WHERE u.firstname = :firstname"),
@NamedQuery(name = "User.findByLastname", query = "SELECT u FROM User u WHERE u.lastname = :lastname")})
public class User implements Serializable
    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "FIRSTNAME")
    private String firstname;
    @Column(name = "LASTNAME")
    private String lastname;

    public User() {

    public User(Integer id) { = id;

    public User(Integer id, String firstname, String lastname) { = id;
        this.firstname= firstname;
        this.lastname = lastname;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getFirstname() {
        return firstname;

    public void setFirstname(String firstname) {
        this.firstname = firstname;

    public String getLastname() {
        return lastname;

    public void setLastname(String lastname) {
        this.lastname = lastname;

So once I got this to work, my next step was to write the same thing in Scala, I wrote it like:

import scala.reflect._
import javax.persistence._

class User(@Id @Column{val name="ID"} @BeanProperty val id:Integer,
           @Column{val name="firstname"} @BeanProperty val firstname: String,
           @Column{val name="lastname"} @BeanProperty val lastname:String)


To make this work from my Java code, I made sure I pointed to my scala User class instead of Java, this includes the persistence.xml file as well. When I tried to run this, I got the following error:

Exception [EclipseLink-0] (Eclipse Persistence Services – 1.0.1 (Build 20080905) ): org.eclipse.persistence.exceptions.IntegrityException Descriptor Exceptions:

Exception [EclipseLink-63] (Eclipse Persistence Services – 1.0.1 (Build 20080905 )): org.eclipse.persistence.exceptions.DescriptorException Exception Description:

The instance creation method [scalajpa.User.], with no parameters, does not exist, or is not accessible.

Internal Exception: java.lang.NoSuchMethodException: scalajpa.User.() Descriptor: RelationalDescriptor(scalajpa.User –> [DatabaseTable(USER)])

So I realised I needed a default constructor for my User method, so I changed my scala class to:

import scala.reflect._
import javax.persistence._

class User
    @Column{val name="ID"}
    @BeanProperty var id:Integer= _

    @Column{val name="firstname"}
    @BeanProperty var firstname: String = _

    @Column{val name="lastname"}
    var lastname:String = _

This worked out perfect. Talk about missing the obvious :-). I realised I had to make this change when I looked here.

While I really like the compactness I got out of it, I’m not too thrilled about having to create a new Scala project, where I need all the EclipseLink libraries on the classpath, then build the Scala project into a jar and put it on the classpath of the JPA application. The whole process would be much easier if I could just create a Scala class right inside my JPA application and have it accessible from other java classes. I tried this and unfortunately the java classes did not recognize the Scala class :(.

As a next step, I plan to get a JSF + JPA application working with whatever help they can get from Scala.

About these ads

9 Responses to “Scala with JPA”

  1. Hi Vikas,

    I’ve added a link to your blog from the EclipseLink wiki ( Feel free to add links on the wiki to any other blogs you might write about EclipseLink and Scala.

    If you’re interested, we have the perfect place for you to further explain how to use Scala with EclipseLink (!


  2. [...] my JPA beans were written as I have done before. A more detailed explanation of JPA for scala can be found [...]

  3. dave said

    Out of curiosity, why use @BeanProperty in combination with field access JPA annotations?

  4. Martin said

    I would like to map an Enum with eclipselink. I saw examples how I’m able to do this with hibernate. Can anybody give me a hint ?


  5. [...] see on github now look a bit different.  During this process I did use some hints from a few other related blog [...]

  6. [...] Scala with JPA February 20098 comments 4 [...]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: