Thursday, May 24, 2007

Java EE5 Persistence and Generics

The new Java EE5 Persistence provides some really cool functionality. It was designed with backwards compatibility in mind. This has resulted in some interesting issues. For example if you perform a Query which returns more than one object, the objects are returned as a List. The List is a very generic Object List. Generally we know that if we perform a query that it should return specific types back to us. For example we expect it to return a collection of the entity itself. If we accept the default which is to return a List, the compiler will report an unchecked warning. If you use the -Xlint:unchecked option, it will point out that we are getting a generic Object List and it can not verify the type of the Object.

The Solution... is simple...

When you get the List back cast it into a specific type. For example:

Query q = em.createQuery("SELECT OBJECT(p) FROM PuzzleBox p");
List puzzles = q.getResultList();
A rather elegent solution... alternatively you can have a more complex version such as

ArrayList puzzles = new ArrayList(q.getResultList);

This works because an ArrayList can accept a Collection as noted in the Javadocs:

ArrayList(Collection<? extends E> c)

The last and most inelegent solution is to annotate the methods which use the generic List of Objects to suppress the complier warnings with a @SuppressWarnings("unchecked") annotation.

Java EE5 Persistence Annotations for Web Applications

I am taking the time to create some cliff notes from a wonderful presentation at JavaOne TS-4593 "Guidelines, Tips, and Tricks for Using Java EE5 from Java Blueprints" (Inderjeet Singh, Marina Vatkina, and an article in JavaOne Today by Rick Palkovic. When the technical sessions are released to the general public, it would be a good read for any Java EE5 developer.

The problem has to do with thread safety on web applications. If you are using servlets, or a technology based on servlets like JSF, you must consider thread safety for persistence. A common injection is to use the container managed persistence context. This usually takes the following form

private EntityManager em;

The problem is this is NOT thread safe if used in a servlet. A servlet processes many requests and the container handles many requests. Even if you use an injection to get a UserTransaction, it lulls the programmer into a false sense of safety.

In their talk, they mentioned four methods to ensure thread safety on servlet based technologies
  • Use a request-scoped bean Java Server Faces. The life of the bean is a single transaction.
  • Use class level annotations with JNDI lookups
  • Use a @Resource annotation
  • Use the "old style" JNDI InitialContext lookup method
Here is an example of the "old-style" method:

public EntityManager getEntityManager() throws NamingException {
Context ic = new InitialContext();
return (EntityManager) ic.lookup("java:comp/env/EM");
Another alternative is to use the @PersistenceUnit annotation to get an EntityManagerFactory. The EntityManagerFactory is a thread-safe object. From the factory we create the EntityManager. This is also thread-safe since it creates a new EntityManager per call.

private EntityManagerFactory emf;

public EntityManager getEntityManager() {
return emf.createEntityManager();
These methods should help the developer maintain thread-safety.

Wednesday, May 23, 2007

Netbeans 6.0 Visual Web Pack (Woodstock) - Common Tasks Component

There are some really cool new woodstock components available in the new VWP and from the site. Woodstock is the original code name for the components which are used in VWP. These new components provide a new level of reusable web components which were not available before. One of the new components which is used extensively in project glassfish on the administrative console is the common tasks component.

This component provides a number of visual queues to what a user can do with the component. The component looks like this currently in Netbeans 6.0 M8. The component is not on the visual palette in 6.0 M9 Preview. The component does not necessarily look appealing in the current form on the palette, but renders perfectly.

As you can see from the images this is a really cool idea. The info button "i" allows you to provide additional information about what the task does. You can add a URL for additional information and a separate title for the info that is displayed. Here is an example from project glassfish where you can see another example of its use.

This shows you some of the capabilities.

The trick is how do you use it?

On project glassfish, when you click on the task it directs you to the appropriate category you selected. The details are scarce. So let me give you one solution. Use the actionExpression tag to set an action for the component. Here is mine for the example I showed above

id="commonTask1" infoLinkUrl="/faces/Page3.jsp"
infoText="This example shows how to use a DropDown component to set values in a table" infoTitle="Master-Detail Example"
style="height: 48px; width: 250px" target="_self" text="Master Detail Example"/>

The actionExpression is bound to a method which uses a navigation handler to navigate to the appropriate page.

public String masterDetailTaskAction() {
handleNavigation(FacesContext.getCurrentInstance(), null, "MasterDetail");
return null;

This allows me to navigate as required. I hope this example will inspire you to try out the new components. They will add a level of professionalism to your web pages and make your users have a better experience on your site. Implemented fully, it may even avoid a call to the help desk, or you to figure out what a task is supposed to do.

Popular Posts