JRuby/Rails with JPA on Glassfish V3
By Ming Zhang on May 23, 2008
JRuby/Rails by default uses ActiveRecords package for the purposes of Object Relational Mapping (ORM). JPA is a JavaEE ORM technology and the advantage of JPA is its pluggability. Modeling class and database migration in Rails are what Entity class and persistence provider do in JavaEE.
The TP2 of GlassFish V3 was released just before 2008 JavaONE and the default persistence provider of V3 is Eclipse Link. It'll be interesting to see how easy it is to use JPA in a classic blog rails application. Vivek wrote in his blog that he used JPA and Rails on GlassFish V2 with TopLink as persistence provider. Here I would modify the blog demo application in his blog to fit for GlassFish V3 and Eclipse Link.
Install JRuby and Rails
- Download the JRuby 1.0.3 zip file . I am setting up everything on Solaris 10 Sparc platform.
- Unzip the file and set the JRUBY_HOME to [Install_Dir]/jruby-1.0.3
- Include [JRUBY_HOME]/bin in PATH for convenience.
Install Rails 1.2.6:
gem install rails -v=1.2.6 --no-rdoc --no-ri -–include-dependenciesThe latest Rails release 2.0.2 has a lot of differences with Rails 1.2.6. In this blog, JRuby1.0.3/Rails1.2.6 will be used and I'll write a follow up blog for the combination of JRuby 1.1.1/Rails 2.0.2.
gem install warblerWarbler is needed for creating war file from JRuby application.
Setup GlassFish V3 and MySQL
- Download TP2 of GlassFish V3 and unzip.
Add JRUBY_HOME value to the GlassFish V3 config file. For example, in my case add
JRUBY_HOME=/opt/jruby-1.0.3to the bottom of [Install_Dir]/glassfishv3-tp2/glassfish/config/asenv.conf
- Setup the MySQL community server by following the MySQL menu.
- Create a new DataBase instance named "test" in MySQL.
- Copy the MySQL connector j driver to [Install_Dir]/glassfishv3-tp2/glassfish/lib.
- Set JAVA_HOME and include [JAVA_HOME]/bin to path. JDK 1.6.0_06 was used in this blog.
Start the GlassFish V3:
Create Java Entity Beans
Create Java Entity bean by following the steps described in Vivek’s blog. A short cut is to download the Entity code and import the code to Netbeans 6.1. The persistence provider in persistence.xml needs to be changed to Eclipse Link as shown below:
If the image appears small in your browser, please right click the image and choose "View Image" to see details. Build the project with “Shift + F11” and a new Post.jar will be created under Post/dist dir.
Create the Rails Application
- Download the blog demo Rails application. Unzip the file and change directory to [Install_Dir]/jpa_blog/WEB_INF. Create a directory called classes.
- Add the Java Entity beans by unjar the Post.jar.
- Use warbler to package the Rails app into a war file: cd [Install_Dir]/jpa_blog; jruby –S warble
Deploy the Blog Demo Rails Application
Deploy the repackaged Rails application onto GlassFish V3:
[Install_Dir]/glassfishv3-tp2/glassfish/asadmin deploy jpa_blog.warIn the GlassFish V3 server.log, the following message should display if the deployment is successful:
[#|2008-05-22T18:42:08.977-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Info: using runtime pool timeout of 30 seconds|#] [#|2008-05-22T18:42:08.979-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no initial runtimes specified.|#] [#|2008-05-22T18:42:08.981-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|PWC1412: WebModule[/jpa_blog] ServletContext.log():Warning: no max runtimes specified.|#] [#|2008-05-22T18:42:09.009-0700|INFO|GlassFish10.0|javax.enterprise.system.container.web|_ThreadID=12;_ThreadName=Thread-5;|Loading application jpa_blog at /jpa_blog|#] [#|2008-05-22T18:42:09.034-0700|INFO|GlassFish10.0|javax.enterprise.system.tools.deployment|_ThreadID=12;_ThreadName=Thread-5;|Deployment of jpa_blog done is 15526 ms|#]
Then, access the blog application at: http://[HOST_NAME]:8080/jpa_blog/blog and the application should display:
Click “New post” link to create new blog entry:
Click “Create” button to submit the blog and the application will show the new blog created and a list of blogs created before: