Scala Nature in Eclipse

Since I’ve been trying to have Scala help with simplifying Java code, I’ve been looking into the possibility of having java and scala classes in the same project(under netbeans and Eclipse). While Netbeans doesnt seem to have this functionality available yet,  someone pointed out the “scala nature” functionality in Eclipse.

Basically, if you have a Java project, you can do a right click -> Scala -> Add Scala Nature. Doing so puts the scala libraries on the classpath and converts the project to a Scala project. This is supposed to allow one to have both Java and scala code in the same project and have them refer to each other.
I’ve been trying this. Here’s what I have:
1.  Scala objects are not visible from java classes, only scala classes are, this is apparently a known problem. I dont see why it would a problem allowing the access of the object’s methods like one would access static methods in Java.
2. When I right click -> Scala -> add Scala Nature for a JavaEE project, it does not add the scala libraries to the project classpath. So this does not solve my problem of having Scala code in Java Web apps.
3. So adding Scala nature only works for regular Java Projects for now.

Its a start though, and if we can access Scala objects and have JavaEE web apps which scala code in them, it’ll bring us one step closer to JavaEE nirvana :-).

JSF 2.0 on glassfish v3 prelude

I decided to start using JSF 2.0 after Dr. Horstmann’s positive review of its features over the last few posts in his blog. Since my current set up is Netbeans 6.5 + glassfish v3 prelude as the server, I decided to update the JSF implementation to 2.0 in glassfish.

Following Dr. Horstmann’s instructions here, I downloaded the latest nightly build jars from here and copied over the 2 jars into glassfish. However, the update tool did not reflect this copy and still showed that I had JSF 1.2.

Since this didnt work, I next decided to run the updater jar and this ended up giving me the same error which was reported here on java.net.

Finally, Dr. Horstmann told me he discovered that deleting the existing JSF 1.2 implementation from glassfish v3 prelude allows the user to then add JSF 2.0.9 as an add on through the update tool. Hallelujah! This finally worked.

I’m really surprised though that the updater jar they expect to work so consistently failed for me and a couple other people who reported the same problems. Also, life would’ve been so much easier if glassfish allowed the user to update to JSF 2.0.9 without having to go through all the circus of manually trying to update the existing set up.

Anyway, if wishes were horses……

Scala with JPA

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 java.io.Serializable;
import javax.persistence.*;

@Entity
@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 u.id = :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;
    @Id
    @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) {
        this.id = id;
    }

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

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.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._

@Entity
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._

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

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

    @Column{val name="lastname"}
    @BeanProperty
    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.