You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Peterus Greatus <pe...@gmail.com> on 2009/03/09 01:07:26 UTC

How do my Actions get to Hibernate so they can persist my objects?

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!

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.

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
http://struts.apache.org/2.0.11/docs/struts-2-spring-2-jpa-ajax.html
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 ?
 
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.

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.

I read this guys post here
http://mail-archives.apache.org/mod_mbox/struts-user/200709.mbox/ 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.

Any help so I can get back to making my web app would be much appreciated.

Thanks,
Peter

-- 
View this message in context: http://www.nabble.com/How-do-my-Actions-get-to-Hibernate-so-they-can-persist-my-objects--tp22404876p22404876.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How do my Actions get to Hibernate so they can persist my objects?

Posted by Dave Newton <ne...@yahoo.com>.
Wes Wannemacher wrote:
> [...] but if Spring is your first venture, bare with it a bit,

Please make sure your webcam is turned off.

I'd add that even for smaller apps the AOP TX management is nearly 
always worth it, and handling JDBC/Hibernate/iBatis/whatever via Spring 
almost always end up being easier, and better, particularly with regards 
to testability.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How do my Actions get to Hibernate so they can persist my objects?

Posted by Peterus Greatus <pe...@gmail.com>.


Wes Wannemacher wrote:
> 
> 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.
> 

Hi Wes,
 I took your advice and read Spring in Action to get a handle on what is
going on. Its pretty clear to me now how it can be the missing-link between
Struts2 an Hibernate. I have to say I'm glad I took the time to get to grips
with Spring - it seems pretty handy.


Wes Wannemacher wrote:
> 
> 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.
> 

The other part of this patch work is that the developers of Hibernate,
Spring and Struts2 have accommodated each other by providing integration
points.
Struts2 says I'll let you use an object factory if you want. In steps
Spring.
Hibernate says you can use my Session Factory to make session objects.
Spring covers this.

What troubled me was in Spring I couldn't see how it used Hibernate - in the
config it does not reference Hibernate classes - only its own Hibernate
module - which in turn (I now know) relies on Hibernate being in the
classpath.

In the Struts2 examples I was looking for programmatic dependencies to make
sense of the relationships ("where are the imports?"). DI via Spring makes
it a configured and configurable (wiring) dependency.

Thanks for helping me make sense of this. I'm new to DI and I started out
wanting to use Struts2 - all the examples in books heavily rely on
additional tools (Ant, Maven) and frameworks. Its getting the complete
system configured that has taken so much time versus using Struts2. They all
aim to get the whole system going on one step. From a learning point of view
I don't care if I throw away code that is tightly coupled because I didn't
follow a flexible design pattern. The code served a purpose - to help me
learn so when I develop the next part/version I see the value in the more
powerful tools (rather than use them because all the cool kids are). What I
wanted from the books I read was for an outside-looking-in explanation of
all the parts to the system. I had to get inside to understand it all. I
think this is missing from all the blogs and tutorials out there. 

Cheers,
P



>
> I read this guys post here
> http://mail-archives.apache.org/mod_mbox/struts-user/200709.mbox/ 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

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



-- 
View this message in context: http://www.nabble.com/How-do-my-Actions-get-to-Hibernate-so-they-can-persist-my-objects--tp22404876p22459292.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How do my Actions get to Hibernate so they can persist my objects?

Posted by Wes Wannemacher <we...@wantii.com>.
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
> http://struts.apache.org/2.0.11/docs/struts-2-spring-2-jpa-ajax.html
> 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 - 

http://code.google.com/p/struts2inpractice/source/browse/#svn/trunk/ch04ex01
 
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
> http://mail-archives.apache.org/mod_mbox/struts-user/200709.mbox/ 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

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org