Return-Path: Delivered-To: apmail-struts-user-archive@www.apache.org Received: (qmail 76294 invoked from network); 9 Mar 2009 00:46:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 9 Mar 2009 00:46:45 -0000 Received: (qmail 8770 invoked by uid 500); 9 Mar 2009 00:46:35 -0000 Delivered-To: apmail-struts-user-archive@struts.apache.org Received: (qmail 8741 invoked by uid 500); 9 Mar 2009 00:46:35 -0000 Mailing-List: contact user-help@struts.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Struts Users Mailing List" Reply-To: "Struts Users Mailing List" Delivered-To: mailing list user@struts.apache.org Received: (qmail 8730 invoked by uid 99); 9 Mar 2009 00:46:35 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Mar 2009 17:46:35 -0700 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [74.125.44.28] (HELO yx-out-2324.google.com) (74.125.44.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Mar 2009 00:46:26 +0000 Received: by yx-out-2324.google.com with SMTP id 31so595931yxl.17 for ; Sun, 08 Mar 2009 17:46:04 -0700 (PDT) Received: by 10.100.197.3 with SMTP id u3mr1640923anf.62.1236559563820; Sun, 08 Mar 2009 17:46:03 -0700 (PDT) Received: from wesw-laptop.localnet (cpe-98-31-31-91.woh.res.rr.com [98.31.31.91]) by mx.google.com with ESMTPS id c1sm6687611ana.0.2009.03.08.17.46.03 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 08 Mar 2009 17:46:03 -0700 (PDT) From: Wes Wannemacher Reply-To: wesw@wantii.com Organization: WanTii, Inc. To: "Struts Users Mailing List" Subject: Re: How do my Actions get to Hibernate so they can persist my objects? Date: Sun, 8 Mar 2009 20:46:00 -0400 User-Agent: KMail/1.11.1 (Linux/2.6.27-13-generic; KDE/4.2.1; x86_64; ; ) References: <22404876.post@talk.nabble.com> In-Reply-To: <22404876.post@talk.nabble.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200903082046.01212.wesw@wantii.com> X-Virus-Checked: Checked by ClamAV on apache.org 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 =E2= =80=93 I > really enjoyed it and now I=E2=80=99ve to write a new web app so I chose = Struts2. > Over the past two weeks I=E2=80=99ve spent more time learning Hibernate, = Ant, > something called Maven, I bought a Struts book that use Maven and now I= =E2=80=99ve > to understand something called Spring! Spring is not as hard as it appears. The Spring libraries are dauntingly bi= g,=20 but in reality, the core of spring is actually quite easy to understand, yo= u=20 just have to wrap your mind around Inversion of Control. It will come=20 naturally if you've used it before, but if Spring is your first venture, ba= re=20 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=E2=80=99s not programming its learning frameworks! > > Anyway back to my question. I understand Struts2 quite well now, I can ma= ke > forms and produce success pages. Everything is transient. I now need to a= dd > 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 h= ow > that works=E2=80=A6. > > What I don=E2=80=99t 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...=20 Struts does not concern itself with things such as database transactions (o= r=20 any transactions). In most applications, you'll find that you "wire" up thi= ngs=20 like a Spring service into your application, then by way of the Spring plug= in,=20 your action will automagically retrieve an instance of the service through = a=20 setter on your action. More below. > > I figure this is where Spring comes in with Inversion of Control or some > other concept I=E2=80=99ve 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=E2=80=99m already defining a bunch of Hib= ernate > mapping xml file for each object I want persisted=E2=80=A6 I'm writing mo= re XML > than Java. > Also how come I=E2=80=99m defining the database connection details in Spr= ing when I > have them in my hibernate.cfg.xml ? =46irst off, rather than making a "service," think of it as a DAO. This is= =20 actually a good pattern to follow. On top of that, if you're pretty handy w= ith=20 the latest Java features, you can build a DAO pretty easily using generics.= I=20 have a full example similar to the one you mentioned available here -=20 http://code.google.com/p/struts2inpractice/source/browse/#svn/trunk/ch04ex01 =20 The difference between my example and the one you referred to is that I use= a=20 generic dao. In fact, this will sound like a shameless plug for my book, bu= t I=20 spend the first couple of chapters trying to explain how all of these piece= s=20 fit together.=20 > > I think what is lacking is an explanation of why certain things are done > the way they are (it=E2=80=99s supposed to be a tutorial=E2=80=A6for begi= nners). 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= =20 makes an attempt to cover a particular design pattern. In the case of=20 applications like you mentioned, you have something similar to the followin= g - Struts2-> Spring-> JPA or Hibernate MVC IoC/DI Database Persistence There are significant advantages to adopting solid design patterns to solve= =20 problems. The learning curve can be steep, but you reap the benefits of=20 reusability, testability and stability.=20 > > I understand Tomcat, Web Apps with Servlets & JSPs. Spring seems to wrap > itself around Hibernate. Tomcat somehow makes Spring go, which in turn do= es > the Hibernate bit. I don=E2=80=99t see how my Actions in Tomcat get to th= e database > via Hibernate any easier with Spring. The piece you're missing here is that Spring services can easily be wired i= nto=20 your actions. If you have a bean named springBean defined in your=20 applicationContext.xml, then have the spring plugin installed in your strut= s=20 app. Then, you have a setter on your action called=20 setSpringBean(SpringBeanInterface sbi), the plugin will see the setter and= =20 give you a copy of the service. It's pretty slick and saves you quite a bit= of=20 time and exception handling. > > I read this guys post here > http://mail-archives.apache.org/mod_mbox/struts-user/200709.mbox/ So I=E2= =80=99m > 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=20 project. It is definitely another layer of complexity, but it is worth it i= f=20 you are building an application to scale up over time. At the same time, th= e=20 more you use it, the more you'll find it helping you. The wiring of=20 dependencies is just one piece, there are also quite a few other helpful=20 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=20 with the intention to introduce people into modern development techniques l= ike=20 this. I take the approach that Struts is a common piece, but that most=20 applications will have the need to utilize other libraries such as Spring,= =20 Guice, EJB or any number of other helper frameworks.=20 =2DWes =2D-=20 Wes Wannemacher Author - Struts 2 In Practice=20 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