RSS Feed Subscribe to RSS Feed

 

Running Hibernate unit tests with Maven

I recently converted a project I have been working on to use Maven. After setting up all the dependencies in the pom, I got everything compiling fine but ran into problems getting the unit tests to pick up the hibernate config (hibernate.cfg.xml) and hibernate mapping (*.hbm.xml) files. With hindsight, it is straightforward, but it took me a while to figure it out so I thought I’d post the solution here.

Initially, I had my hibernate.cfg.xml file in the following directory

my-app/src/main/java

And when I first tried to run my hibernate unit tests (mvn test), I got this error:

SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found

Some checking of the maven docs and some forums revealed the following possible solutions:
1) Copy the hibernate.cfg.xml file to

my-app/target/classes

This works, however I think it is a hack as the target folder is generated rather than being somewhere you should actually manually create files.
2) The next solution I found suggested moving the hibernate.cfg.xml to

my-app/src/main/resources

This works well as the contents of this folder are copied to the base level of the my-app/target/classes folder so it is basically a ‘more correct’ solution than the first.
3) The next and I think best solution was to create a new, duplicate hibernate.cfg.xml inside

my-app/src/test/resources

This allows a different hibernate.cfg.xml file to be used for testing and, for example, facilitates connecting to a different database (such as hsqldb) for testing while continuing to use your regular database (such as MySQL) for the app itself.

A couple of points to note:

1) The hibernate mapping files (i.e. the *.hbm.xml files) should also be in the resources folder (in whatever directory structure you choose) to ensure that these too are accessible by the unit tests.

2) I am not (yet) using Spring with the hibernate components, which would likely have changed the above setup.

Refs/links

http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

http://www.mail-archive.com/users@maven.apache.org/msg54720.html

Tags: , ,

JavaFX Meetup

I attended another one of the excellent San Francisco JUG meetings tonight. This time the topic was Java FX and WidgetFX. Java FX is Sun’s new platform for delivering Rich Internet Applications (RIAs), competing in the same space as Adobe’s Flex and Microsoft’s Silverlight.

It is a fairly new technology with the first 1.0 release of the Desktop platform just happening last December. The Sun web site seems to show the Mobile platform as being available now too. Wen Huang from Sun talked about the planned 1.1 release, but was strangely coy when asked about the ‘TV platform’ that is also planned. He mentioned a tie in with Sony and Bluray so I guess we an expect more details on that in the near future.

Wen explained that JavaFX is built on top of Java, in that it runs on top of the Java Plugin and hence it can access to all the usual Java libraries.

Doris Chen, a Staff engineer and technology evangelist at Sun, then took over the presentation and got a little more technical. She quickly walked us through a couple of demos involving animation and videos in the form of puzzle games. It did look pretty cool. She also demonstrated how Java FX apps could be dragged out of the browser and continue to run independently, even when the browser was then closed. This is possible due to each widget running in its own Java VM, independent of the browser. I did notice those annoying Certificate security warnings popping up during the demo startup though which made me wonder… Would using JavaFX be as seamless for the end user as Flash, or would there be security, download, startup and plugin compatibility issues that applet usage sometimes involved?

Next up, Stephen Chin took over. Stephen is the co-author of a soon to be published book called Pro JavaFX book. He is also closely involved with two open source JavaFX libraries, JFXtras and WidgetFX. He walked us through a couple of code examples. The things that jumped out at me most about JavaFX were

  • the declarative construct used for creating objects, which I am not too familiar with (similar to the Scala approach maybe?)
  • extensive use of a ‘bind’ operator which seems to ensure that variable value changes get reflected in the UI
  • JavaFX doesn’t seem to throw many exceptions, instead it tries to handle them as gracefully as possible
  • It is a Functional language: Functions are first class entities that can be assigned to variables, passed in and returned from functions

The class finished with Stephen walking us through some hands on examples, using NetBeans (Update: see my comment at the end of this article on the JavaFX Eclipse plugin) and both WidgetFX and JFXtras.

Overall, I thought the presentations were very good and provided a great overview, and some of the details, of JavaFX. For me personally though, none of the projects I am currently working on have a requirement for ‘rich’, multimedia type content, but it would be nice to create a few more mundane desktop type JavaFX GUIs to see how it fares compared to the Swing GUIs I have created in the past.

I do have some concerns about how to integrate JavaFX GUIs with the web tier and backend. Doris Chen tonight mentioned JSON integration, but there were less details on how JavaFX would integrate with model-2 MVC libraries like Spring MVC or Struts. I will definitely watch the JavaFX space closely to see how widely accepted it becomes, but I am also interested in learning more about other Java GUI technologies that I haven’t had too much exposure to yet, such as JSF and Facelets. Perhaps in another JUG meetup soon…

Tags: , , ,

Persisting Money class with Hibernate

Further to my previous post about representing monetary amounts in Java, I have been using the TimeAndMoney library.

I ran into some problems persisting my classes that use Money via Hibernate. I should point out that these are not problems with the Money class or the timeandmoney library, rather than with my hibernate setup, but I thought I would post my approach here for future reference.

I started by incorrectly trying to map my Money field (in my case, called ‘openingBalance) as a property, viz:

<property name=”openingBalance” type=”com.domainlanguage.money.Money”>

Which resulted in the following error:

SEVERE: Data truncation: Data too long for column ‘openingBalance’ at row 1
Exception in thread “main” org.hibernate.exception.GenericJDBCException: could not insert…

Then I tried mapping it as a component, viz

<component name=”openingBalance” class=”com.domainlanguage.money.Money”>

<property name=”amount”/>

<property name=”currency”/>

</component>

But this resulted in the following error:

Initial SessionFactory creation failed.org.hibernate.PropertyNotFoundException: Could not find a setter for property amount in class com.domainlanguage.money.Money
Exception in thread “main” java.lang.ExceptionInInitializerError

This is because the Money class has no setAmount or setCurrency methods. The solution is to specify field access (as opposed to accessor method access), as in

<component name=”openingBalance” class=”com.domainlanguage.money.Money”>

<property access=”field” name=”amount”/>

<property access=”field” name=”currency”/>

</component>

Note that if you check the Money javadocs, it (correctly) says that the setters are not provided because they would break encapsulation They do however (and I quote, “begrudgingly”) provide alternative setters such as setForPersistentMapping_Amount, but I am not sure how to get Hibernate to use these, and in this case, I don’t think it is necessary or beneficial anyway.

Tags: , ,

Mylyn

I’ve started using Mylyn. It is a “task-focused interface for Eclipse that reduces information overload and makes multi-tasking easy”. Its objective is to improve productivity by reducing searching, scrolling, and navigation.

What does that mean? Basically, it allows you to access your bug tracker from inside Eclipse and then for each task/bug you are working on, it remembers what files you have been using most. The result is that when you switch back to a task you were working on before, the important and most relevant files you were using will be reopened and so at any given time, the files you need are close to hand.

The ‘tasks’, in my case, are defects/enhancements assigned to me in Trac (or specifically DevJaVu), but it has integration with Bugzilla, JIRA and others.

It’s not exactly ground breaking or revolutionary, but it is definitely useful (although the integration/synchronisation with Trac does seem to be a bit flaky, but maybe that is an issue with DevJaVu).
Next up, I would like to start making more use of the change set support.

Tags: , , ,

Workflow Management: JBoss’s JBPM

The project I’m currently working on had a requirement for a workflow engine to manage the various process flow options for an order management system. For example, an order can be created, modified, submitted etc and we need to be able to persist and retrieve the process at any stage as we wait for human involvement to move to the next stage. I have been evaluating JBoss’s JBPM. It seems to meet our requirements but has been very difficult to get up and running. Overall it has been a frustrating process so far. The main JBoss JBPM site is at here and theServerSide has a good article on using it with Spring/Hibernate integration.

Tags: , , , , , , ,

Spring talk from Rod Johnson

I attended a talk on Spring on Monday, given by the creator himself, Rod Johnson. He focused mainly on the new annotations features of Spring 2.5 which was interesting, but it was also good to hear an example of how Open Source Software funds itself. Basically SpringSource (of which Rod is CEO) as a company do the vast majority of the code contributions to Spring (and apparently to Tomcat as well), but get their revenue from things like their Enterprise level pay-for offerings such as their SpringSource Enterprise as well as support and training.

The talk was at the Google offices in Mountain View and co-hosted by the San Francisco Java User Group.

Tags:

POJOs in Action

My latest reading material is POJOs in Action – a good book about developing Enterprise Applications using Lightweight Frameworks such as Spring, Hibernate and JDO. It basically advocates an alternative approach to using heavyweight containers such as EJBs (although they seem have improved a lot since the release of EJB3).

I was also able to meet the author, Chris Richardson, after attending my first San Francisco Java User Group meeting.

Tags: ,

NetBeans IDE

I have been playing around with the latest NetBeans IDE (v6.1) recently, mostly as part of some JEE and  Glassfish research I’ve been doing. I am a long time Eclipse user (even back to its predecessor, VisualAge for Java), but I have been pleasantly surprised by NetBeans. It’s UI is definitely a litlle clunkier than Eclipse, but it is fairly intuitive and easy to use and it also seems to useful GUI builder tool that I would like to experiment with more.

See some reviews here and here.

Overall, interesting and I’d like to use it some more, but I still have no plans to stop using Eclipse as my main IDE.

Tags: , , ,