struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wes Wannemacher <>
Subject Re: How do my Actions get to Hibernate so they can persist my objects?
Date Mon, 09 Mar 2009 00:46:00 GMT
On Sunday 08 March 2009 20:07:26 Peterus Greatus wrote:
> I am hoping someone can paint a picture for me as I do not really
> understand the relationship between Struts2 + Spring + Hibernate. The
> reason I know about Spring is every book or tutorial I can find with
> Struts2 and persistence uses Spring.
> Background is my last programming in Java was 9 years ago with Struts – I
> really enjoyed it and now I’ve to write a new web app so I chose Struts2.
> Over the past two weeks I’ve spent more time learning Hibernate, Ant,
> something called Maven, I bought a Struts book that use Maven and now I’ve
> to understand something called Spring!

Spring is not as hard as it appears. The Spring libraries are dauntingly big, 
but in reality, the core of spring is actually quite easy to understand, you 
just have to wrap your mind around Inversion of Control. It will come 
naturally if you've used it before, but if Spring is your first venture, bare 
with it a bit, you'll be glad you did.

> I thought this was going to be easier than Struts 1 with JDBC and writing
> my own SQL.
> It’s not programming its learning frameworks!
> Anyway back to my question. I understand Struts2 quite well now, I can make
> forms and produce success pages. Everything is transient. I now need to add
> some persistence so I am using Hibernate (as its seems to be the bees
> knees). After much reading of a large book about Hibernate I understand how
> that works….
> What I don’t get is how my Struts2 action talks to Hibernate.
> I've added a Listener to my web.xml for Hibernate, but not sure when that
> is used.
> I have a HibernateUtil that creates a SessionFactory which I understand is
> used for transactions with the database. I want that guy to start when my
> web app is started in Tomcat so all I have to do is go to him to make a DB
> connection.
> But how do I get a session from the factory within a Struts2 Action? It
> seems odd to put code into each Action to do this. I imagine people more
> clever than I solved this problem.

In a way, you are answering your own question about how Spring fits in... 
Struts does not concern itself with things such as database transactions (or 
any transactions). In most applications, you'll find that you "wire" up things 
like a Spring service into your application, then by way of the Spring plugin, 
your action will automagically retrieve an instance of the service through a 
setter on your action. More below.

> I figure this is where Spring comes in with Inversion of Control or some
> other concept I’ve started reading about.
> I've read this guy
> This PersonService object seems like a hokie solution. Do I make a Service
> for each object I want to persist? Thats a bit mad. I have to define all of
> these services in Spring too? I’m already defining a bunch of Hibernate
> mapping xml file for each object I want persisted… I'm writing more XML
> than Java.
> Also how come I’m defining the database connection details in Spring when I
> have them in my hibernate.cfg.xml ?

First off, rather than making a "service," think of it as a DAO. This is 
actually a good pattern to follow. On top of that, if you're pretty handy with 
the latest Java features, you can build a DAO pretty easily using generics. I 
have a full example similar to the one you mentioned available here -
The difference between my example and the one you referred to is that I use a 
generic dao. In fact, this will sound like a shameless plug for my book, but I 
spend the first couple of chapters trying to explain how all of these pieces 
fit together. 

> I think what is lacking is an explanation of why certain things are done
> the way they are (it’s supposed to be a tutorial…for beginners). Even those
> tutorial writing guys over at RoseIndia don't seem to have nailed this.

The reason things are spiralling the way you noticed is that each framework 
makes an attempt to cover a particular design pattern. In the case of 
applications like you mentioned, you have something similar to the following -

Struts2->	Spring->	JPA or Hibernate
MVC		IoC/DI		Database Persistence

There are significant advantages to adopting solid design patterns to solve 
problems. The learning curve can be steep, but you reap the benefits of 
reusability, testability and stability. 

> I understand Tomcat, Web Apps with Servlets & JSPs. Spring seems to wrap
> itself around Hibernate. Tomcat somehow makes Spring go, which in turn does
> the Hibernate bit. I don’t see how my Actions in Tomcat get to the database
> via Hibernate any easier with Spring.

The piece you're missing here is that Spring services can easily be wired into 
your actions. If you have a bean named springBean defined in your 
applicationContext.xml, then have the spring plugin installed in your struts 
app. Then, you have a setter on your action called 
setSpringBean(SpringBeanInterface sbi), the plugin will see the setter and 
give you a copy of the service. It's pretty slick and saves you quite a bit of 
time and exception handling.

> I read this guys post here
> So I’m
> pretty certain I need to be using Spring.
> To me it seems like Spring is a pre-requisite for Java development. Maybe I
> need to be drinking the Spring Kool-Aid.

The need to use Spring really increases proportional to the size of the 
project. It is definitely another layer of complexity, but it is worth it if 
you are building an application to scale up over time. At the same time, the 
more you use it, the more you'll find it helping you. The wiring of 
dependencies is just one piece, there are also quite a few other helpful 
advantages, AOP, transaction management, and mock objects to name a few.

Again, this will seem like a setup for my book, but I am currently writing it 
with the intention to introduce people into modern development techniques like 
this. I take the approach that Struts is a common piece, but that most 
applications will have the need to utilize other libraries such as Spring, 
Guice, EJB or any number of other helper frameworks. 



Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message