Fabio Cepeda

Archive for February 2008

Doubt:

I develop a java web application that uses a lot of type objects. For example, Object Visit has a property state that could be Programmed, Done, Cancelled, etc… The current solution for this is to have a table in the DB representing the state of the visit and a FK from the table visit to the table with the states. Within the layers of the code the state is manage as an Entity with its own CRUD implemetation.

When you have a lot of object value types this could add up a lot tables in your DB and code in your application. In some cases I guess is ok to have CRUD functionality, but in others I wish the process could be shorter. For example: types that are not reusable and unique to a particular POJO. So, I was thinking of a way to be able to reduce some of the states or types using enum types throughout my application.

Today, I researched about how to map a JAVA 5 Enum type to a MySQL enum in order to be used with a web application that uses Spring.

Here, some of my findings:

I found these good article on how to deal with the Hibernate part, it seems straight forward:

http://weblog.dangertree.net/2007/09/23/mapping-java-5-enums-with-hibernate/

For the Spring part, I guess it is just a matter of writing the PropertyEditor

Doubts still remaining:

Will that solution above, map the Enumeration to a enum type in MySQL? Does Hibernate supports this? Is it better to map the elements of an enumeration to another table in the DB or having it inside your main table as a MySQL enum is better?

Will mapping the JAVA enumeration to the MySQL enumeration saves time when adding a new type?

Here are some disadvanges:

From what I read, it seems you have to recompile your code when you have a JAVA enumeration and you want to add and extra element to it. In the current solution, you only have to add that value in the DB and Hibernate can deal with it without a recompile.

Some Advantages:

Enumeration could make your code cleaner making it easier to code tests for states and types comparissions. Surely it will be less verbose and easier to read.