Friday, June 08, 2007

State Pattern Persistence with Hibernate

This story has moved to NerdBoys.com. Please read this story at its new location.

5 comments:

l0st3d said...

hey Joe,

I found this useful for some work I'd been doing today, so I thought
I'd leave a comment and say thanks. We were trying to persist a state
pattern implemented in a stateless way, with an abstract state class
being extended for multiple states. We have managed to argue our way
into restructuring the state model as a java 5 Enum that has methods
the state transition methods implemented like:

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

This appears to be persisted by hibernate as an integer, without the
need to create a user type, which we felt was a quicker and easier
solution, as the EJB3 world is all a bit new to most of the
development team.

Joe said...

Thanks for dropping by my blog.

> the EJB3 world is all a bit
> new to most of the
> development team

Hehe, just be thankful they don't have to learn EJB2!

cster said...

Hi Joe. I too found this a useful piece based on attempting to implement the State Pattern from the book with the nice girl on the cover :O)

The problem I could not crack was the fact the pattern actually instantiates the states within the entity and passes a 'this' reference in to each state constructor.

You provide an alternative which is to pass the entity reference (e.g. Customer) in to the action method (e.g activate). A nice touch but I wish to retain the original pattern structure since it is cleaner for my requirement: my different states sometimes transition the entity state bit sometimes do not require to do so.

I cannot see any way to use Hibernate UserType to retain the exact pattern implementation but let me know if you think otherwise.

Cheers, Cam

Anonymous said...

org.hibernate.MappingException: Could not determine type for: OrderStateUserType

where i have put OrderStateUserType when i have complicate structure?

Anonymous said...

i got something useful from your blog. thank u