The Source for Java Technology Collaboration
User: Password:



Sahoo's Blog

January 2006 Archives


When can I use hibernate as EJB3 persistence provider in GlassFish

Posted by ss141213 on January 17, 2006 at 01:08 AM | Permalink | Comments (5)

Can I use Hibernate in GlassFish? is a question I have been asked several times in recent past? Well, the answer is Yes & No(as of now), depending on what you are trying to use it as. By the way I must clarify, it's a NO only temporarily. Let's understand why:

Yes:
If you want to use Hibernate as yet another persistence framework, there is already a nice and detailed posting about how to use hibernate in GlassFish. That article shows how to use Hibernate session APIs, hibernate O/R mapping XML files etc. It does not talk about using Hibernate as an EJB 3.0 persistence provider. That's the reason you find so many configuration steps mentioned in that article.

No:
If you are trying to use Hibernate as an EJB 3.0 persistence provider, it is right now not possible. I must say, it's only a temporary no because of the mismatch in interfaces between GlassFish and Hibernate. When I say using Hibernate as an EJB 3.0 persistence provider, what I mean is that user is using EJB 3.0 persistence API(javax.persistence) classes. e.g. using @persistenceContext to inject an EntityManager, @Entity to annotate POJO classes as entity beans, persistence.xml to configure a persistence units etc. as demonstrated in in this example as opposed to using Hibernate session management APIs, Hibernate O/R mapping XML file to convert their POJO class to an entity bean etc. As you probably know the EJB 3.0 (JSR 220) spec defines a Service Provider Interface(SPI) to plug in a compliant persistence provider to a Java EE 5 container like GlassFish. These SPI classes are part of javax.persistence.spi package. If you look at the revision history of the EJB 3 Persistence(a.k.a. Java Persistence API) spec, you will see that (unfortunately) the SPI has been changing quite a lot while the spec is being finalized. So it is hard for any two different projects to keep in synch with each other. Now that the final draft of the EJB 3 spec has been proposed, hopefully(!) we have a stable interface that both GlassFish and Hibernate can soon implement to achieve this pluggability.

EJB 3.0 Persistence SPI
The SPI classes are part of javax.persistence.spi package. Users don't have to know about this package while using Java Persistence API inside a container. It is used by the container and persistence provider to provide the necessary pluggability. This is indeed good news for developers as they can use a combination of their favorite Java EE container with their favorite persistence provider without sacrificing portability of their applications. Some other day I shall talk more the SPI and issues around it.

Some EJB 3.0 Persistence API related resources:

Introduction to using Java Persistence API in Java EE
Latest builds of GlassFish where an open source implementation of this spec is being done.
GlassFish persistence project

More articles about



Custom tags to use Java Persistence API in JSP

Posted by ss141213 on January 02, 2006 at 12:57 PM | Permalink | Comments (0)

In earlier articles we have discussed about using Java Persistence API in a web application and in a multi-tier Java EE application. In this article I shall talk about a few custom JSP tags (the links in that page are broken, but when you download the complete bundle it includes a page where links work.) that I have developed to use Java Persistence API in JSPs. Collectively the tags are referred to as jpa taglib here. It also shows how to inject persistence context into JSPs. A JSP tag library is a collection of custom tags where each tag abstracts out some functionality used by a JSP page. Not only allows a more natural use of that functionality within JSP pages, but also it allows separation of responsibilities.

Source code for the tag library and a sample web application that illustrates the use of the tag library as well as the binaries are available here. Downloaded bundle also include documentation about the tag library that is generated using tlddoc.

Detailed Steps:
Given below are the detailed steps to develop the tag library as well as a sample application that uses the same. Although I am using glassfish in this article, the tag library and the sample are completely portable and can be used in any Java EE 5 compatible application server with a little modification to the accompanying build.xml.

Step #1: Write custom tags

A tag library is a collection of tags. Prior to JSP 2.0, custom tags could only be developed using Java language, but JSP 2.0 allows custom tags to be developed using an Expression Language. I am using Java to develop custom tags. The source code for the tags are in jpa-tag-lib/src directory in package com.sun.jpatl package. of all the tags, jpa:tx tag is TryCatchFinally kind of tag. It is written such that it always ends the transaction, in case of no exception in the embedded JSP fragment, it commits the tx, else it rolls back the tx. All other tags are Simple Tags.

Step #2: Write a tag library descriptor jpa.tld

A tag library descriptor is an XML document describing the tags that are part of the library. Refer to the JSP Spec fpr mode details. As you can see from the tld file, I have given the URI as http://weblogs.java.net/ss141213/tags/jpa.

The tag classes along with tag library descriptor file(jpa.tld) are packaged in a file called jpa-tag-lib.jar which is bundled in WEB-INF/lib dir of the web application.

Step #3: Write a sample web application that uses jpa-tag-lib

Let's write a simple web application that uses these tags to do some database operation. The functionality of this web application is same as the one described in my earlier blog.

Step #3.1: Write entity bean UserCredential.java

Step #3.2: Define a persistence unit persistence.xml

The entity classes along with persistence.xml are packaged in entities.jar which is bundled in WEB-INF/lib dir of the web application.

Step #3.3: Write login.jsp

Points worth noting about this JSP are:
1) It expresses its dependency on jpa-tag-lib by using the following directive:

<%@ taglib uri="http://weblogs.java.net/ss141213/tags/jpa" prefix="jpa" %>

2) It uses custom tag called jpa:injectDefaultPC to inject a container managed entity manager. This entity manager instance is made available in a variable called em1.

3) It uses custom tag jpa:find passing it the entity manager that was earlier injected. Also note how it uses a request time expression to pass the value of the primary key. The searched object is stored in a variable called credential which is later on used by the JSP to compare password.

Step #3.4: Write registration.jsp

Points worth noting about this JSP are:

1) It uses jsp:useBean to initialise a bean called credential by reading attributes passed in request object.

2) It uses custom tag jpa:tx to mark the boundary of a transaction. Inside this transaction, it uses uses custom tag jpa:persist to persist the java bean.

Step #3.5: Write a couple of html files called login.html and registration.html. Note, there is no need to write any web.xml as it is optional in Java EE 5.

Step #4: Build using build.xml

This is a very simple build.xml just to demonstrate the compilation and packaging process. The build targets are:
build-jpa-tag-lib -- builds only the tag library.
build -- builds the tag library build/jpa-tag-lib.jar as well as the sample web app build/web-app6.war. The war file bundles the tag-lib.
clean -- cleans
doc-jpa-tag-lib -- generates documentation for the tag library using a free open source tool called tlddoc. To run this optional target, you need to download the tool from here.
verify -- verify uses a tool called verifier that checks compliance of the application against Java EE spec.
deploy -- deploys the ear file
undeploy -- undeploys the ear file

The last three targets are specific to Sun Java System Application Server 9 PE which is implementing Java EE 5 spec.

As you can see, to compile the sources, only library needed is javaee.jar which contains the Java EE 5 platform APIs.

Step #5: Set up a data source

By default in glassfish entity manager uses the default pre-configured data source with JNDI name jdbc/__default that glassfish comes with. This data source talks to a Derby database called sun-appserv-samples. Refer to the README where I have listed the command needed to start and stop the Derby database. Glassfish has a feature called Java2DB which can autocreate the database schema during deployment, so there is no need to create tables.

Step #6: See them in action
Type http://localhost:8080/web-app6/login.html in the browser. Replace localhost & 8080 by host and port as appropriate in your env.

Want to help?

This is my first attempt to write a tag library, so comments are most welcome. I plan to enhance the tag library and make them available in future. I am even thinking of starting an open source effort to make a production quality tag library for Java Persistence API. Interested readers are welcome to join me in this effort.

References:

Java Server Pages
Tutorial on developing custom tags by Qusay H. Mahmoud
Tutorial on how to use Java Persistence API in a web application
Tutorial on how to use Java Persistence API in a 3 tier (web->ejb->db) Java EE 5 application
Tag Library Documentation Generator Tool: A free open source tool in java.net.

More articles about .





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds