RSS Feed Subscribe to RSS Feed

 

MyMoney – A simple SpringMVC app

I created a small, open source web app called MyMoney for entering and tracking spending details. It allows you to create accounts (for example Cash or Checking) and enter transactions associated with those accounts.

The app is running at http://mymoney.shaunabram.cloudbees.net and the sourecode is available via GitHub.
Read more

Tags: , ,

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: , ,

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: , ,

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: , , , , , , ,