Saturday, April 26, 2008

Hibernate & Java /Hibernate Annotations

Just a quick post on Hibernate ‘@Where’ annotation usage and a note on some odd behavior I’ve seen using Hibernate in Java (my environment NetBeans IDE 6.0 (Build 200711261600) Java: 1.5.0_13; Java HotSpot(TM) Client VM 1.5.0_13-119, System: Mac OS X version 10.5.2 running on i386; jboss: 4.2.2.GA; seam: 2.0.0.GA)

First, annotations:
I had a hard time finding a “pasteable” explanation of the use of @Where
Now that I’ve found how to do it, I thought I’d share.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “itemId”)

@Where(clause = “item_table = ‘competition’”)
public Set getItemTagRatings() {
return this.itemTagRatings;

public void setItemTagRatings(Set<> itemTagRatings) {
this.itemTagRatings = itemTagRatings;
The effect of this is to set up an additional condition upon TagRatings, so that only those TagRatings that have the item_table value = ‘competition’ are retrieved (item_table is the name of the column in the db).

The second item is odd. I cannot determine the underlying cause, so I’m just sharing the symptom and a workaround

The loop below would only execute once (and occasionally I would get a javax.faces.FacesException with the message: “#{competitionHome.persist}: java.util.ConcurrentModificationException”)

The concurrent modification exception was not a particular surprise, but the silent (no visible exception) execution of the loop a single time befuddles me. If supressWarnings wasn’t a compile time operation that’s where I’d place the blame but.... (BTW I have no doubt that I'm doing something deeply wrong in the not working loop below, my goal is to highlight the relatively opaque effect of the error)

Note: I designate the loop as ‘working’ since the query string shown will not work as written, it required some changes to work since the #{newName} did not evaluate to the appropriate value.

The loop

for((String newName : newTagNames){
List tags = (List) em.createQuery(“select t from Tag t where lower(t.tagName) = #{newName}“).setMaxResults(pageSize).setFirstResult(page * pageSize).getResultList();

if ((tags == null) || (tags.size() == 0)) {
newTag = new Tag(newName, user);

Breaking the loop up into multiple loops worked. The ‘working’ loop(s)

for ( String newName : newTagNames) {
ArrayList tags = null;
try {
Query q = em.createQuery(“select t from Tag t where lower(t.tagName) = #{newName} “);
q.setMaxResults(pageSize).setFirstResult(page * pageSize);
tags = (ArrayList) q.getResultList();
} catch ( IllegalStateException e) {
log.error(“error getting tags: “ + e);
} catch ( IllegalArgumentException e) {
log.error(“error getting tags: “ + e);
if ((tags == null) || (tags.size() == 0)) {
Tag newTag = new Tag(newName, currentUser);
for ( Tag newTag : newTags) {
for ( Tag newTag : newTags) {

Friday, April 11, 2008

jboss, seam, jbpm

I’ve been working in the jboss/seam framework for a few months now and recently tried to add jbpm workflow functionality to a project. In attempting to do this, I’ve hit some rough patches that have caused me to place the jbpm parts of the project on hold as they were not “mission critical” to this prototype.

Note that these remarks are in the context of a seam 2.0/jboss 4.2x environment (jboss 4.2+ is required by seam 2+), running on OSX 10.5. The jboss/seam/jbpm combination appears to be the source of the problems, OSX 10.5 appears irrelevant.

Seam offers two ways to utilize jbpm in your applications: the first is pageflow; the second is what the documentation terms an overarching business process.

The seam pageflow model is, as one would expect, a way to map desired page to page transitions using an xml file. It is described as being similar to other pageflow definition languages such as spring web flow but built with a conversation model in mind. The idea is that conversations allow better support for random user navigation e.g., hitting the back button. I cannot comment on that claim, but having used pageflows a bit I’ve found them adequately expressive and useful.

The difficulties that I have had are centered around integrating an overarching business process into my seam application. These business processes may include asynchronous server and user processes executing over long periods of time, e.g., weeks. A simple example is the verification of user identity in a web facing system: A user requests an account on the system, and activates the account by responding to an email sent by the system.

The business process requires the system to send an email to the user following the initial request, validate the response or, in the absence of a response, retire the request/send another reminder

The first issue I had in incorporating this workflow is the classic “who’s on top” problem, since I found that pageflows could not initiate workflows.

My prototyping started with a pageflow, as pageflows are the natural frameworks for handling the request for a new user account. The pageflow structure makes it easy to verify that the requested username/password etc satisfy system requirements. If they don’t, the application can be configured to just sit on the request page and not proceed to subsequent pages.

For example, by enclosing the page redirect in a <> construct, the return of a null from the userHome.persist method keeps the application on the page (there is a protocol (not shown) that allows the application to post error messages on the page).

<navigation from-action=?#{userHome.persist}?>
<redirect view-id=?/User.xhtml?/>

However, as mentioned above, a pageflow cannot initiate a workflow, so it is necessary to enclose the pageflow within a workflow to achieve the desired result. This entails changing the preceding page so that it kicks off a jbpm workflow rather than simply going to the next page.

This is the point at which I realized that I would need tools for monitoring and debugging workflows. The jbpm console appears key to this. Disconcertingly, I could not get the console to run with jboss 4.2.2.GA. This appears to be a general problem. In my case, the instructions on the wiki enabled me to get my workflow initiated without it throwing exceptions but I still was unable to get the console working.

One suggested path was to build the console from source. However, I proved to be unable to get it to build from source with the level of effort I was willing to expend. After reading posts such as this, I concluded that building from source may not be the most productive experience.

The bottom line is that you’re in an odd space working with seam 2.0 -- it needs jboss 4.2X but some of the other redhat/jboss tools don’t yet support jboss 4.2. I’m a bit surprised that this is the case: seam 2.0 GA was released 2007-11-01 06:49 and jboss 4.2.2 was released 2007-05-11.

Since I expect this situation to eventually be rectified, and as I mentioned, workflow was not “mission critical” I decided to wait for a working console application to be posted on the site.

In fairness I’m working from the community “minimally supported” version -- the situation could be radically different in the supported versions.