Results 1 to 2 of 2
- 09-27-2009, 11:14 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
Beginner Spring with Hibernate integrated. Is this code correct
Hello,
I've recently start to learn and integrate Spring and Hibernate to my Java web app (Struts2 as a presentation). I've googled a lot about tutorial and examples and so on. Well, everything seems ok and running based on the materials I've collected. but as long as I've deployed it on a hosting web server and got a Lot of Runtime java.lang.OutOfMemoryError exception, I wanted to clear things up to myself that everything I'm doing is correct as point of view of an expert, then assuming there are some issues about my Webserver Hosting or something else.
So here the Code:
web.xml
struts.xmlJava Code:<web-app> .... <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-hibernate.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> .... </web-app>
applicationContext-hibernate.xmlJava Code:<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="true" /> <package name="default" extends="struts-default" namespace="/"> <action name="first" class="FirstAction"> <result name="index">/web/content/first/index.jsp</result> </action> </package> </struts>
spring-hibernate-actions.xmlJava Code:<beans> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"><value>/WEB-INF/jdbc.properties</value></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>${jdbc.driverClassName}</value></property> <property name="url"><value>${jdbc.url}</value></property> <property name="username"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="configLocation"> <value>classpath:my\classpath\domain\hibernate.cfg.xml</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <import resource="/spring-context/spring-hibernate-daos.xml"/> <import resource="/spring-context/spring-hibernate-services.xml"/> <import resource="/spring-context/spring-hibernate-actions.xml"/> </beans>
spring-hibernate-services.xmlJava Code:<beans> .... <bean id="FirstAction" class="my.com.package.struts.action.FirstAction"> <property name="firstService"><ref bean="firstService"/></property> </bean> <bean id="SecondAction" class="my.com.package.struts.action.SecondAction"> <property name="secondService"><ref bean="secondService"/></property> </bean> .... </beans>
spring-hibernate-daos.xmlJava Code:<beans> .... <bean id="firstService" class="my.com.package.service.FirstServiceImpl"> <property name="firstDAO"><ref bean="firstDAO"/></property> </bean> <bean id="secondService" class="my.com.package.service.SecondServiceImpl"> <property name="secondDAO"><ref bean="secondDAO"/></property> </bean> .... </beans>
FirstAction.javaJava Code:<beans> .... <bean id="firstDAO" class="my.com.package.dao.FirstDAOImpl"> <property name="sessionFactory"><ref bean="sessionFactory"/></property> </bean> <bean id="secondDAO" class="my.com.package.dao.SecondDAOImpl"> <property name="sessionFactory"><ref bean="sessionFactory"/></property> </bean> .... </beans>
Java Code:package my.com.package.struts.action; import my.com.package.domain.First; import my.com.package.service.FirstService; public class FirstAction extends ActionSupport implements ServletRequestAware, ServletContextAware { private FirstService firstService; public String execute() { String successReturn = "index"; First firstDomain = firstService.getContent(); request.setAttribute("firstDomain", firstDomain); return successReturn; } public void setFirstService(FirstService firstService) { this.firstService= firstService; } }
FirstServiceImpl.java
FirstDAOImpl.javaJava Code:public class FirstServiceImpl implements FirstService { private FirstDAO firstDAO; public FirstServiceImpl() {} public First getContent(String lang) { First firstDomain = new First(); List<First> firstContent = firstDAO.getContent(); Iterator<First> firstContentItr = firstContent.iterator(); if ( firstContentItr.hasNext() ) { firstDomain.setDescr(firstContent.get(0).getDescr()); firstDomain.setInfoAvailable(true); } else { firstDomain.setInfoAvailable(false); } return firstDomain; } public void setFirstDAO(FirtsDAO firstDAO) { this.firstDAO= firstDAO; } }
I'd really appreciate if anyone could help me whether I was on a right path or something seems wrong or Exception-Throwing-Material on the above application development.Java Code:package my.com.package.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import my.com.package.domain.First; public class FirstDAOImpl extends HibernateDaoSupport implements FirstDAO { public AboutUsDAOImpl() {} public List<First> getContent(String lang) { List<First> firstContent = this.getHibernateTemplate().find("from First where isDeleted=0"); return firstContent ; } }
Regards,
Khosrow.
- 10-24-2009, 02:13 AM #2
Member
- Join Date
- Oct 2008
- Location
- US
- Posts
- 58
- Rep Power
- 0
Let us know what problem you are facing? What is the error message you get in the logs where you start the server.
Have fun....
JAVA FAQs
Similar Threads
-
LazyInitializationException Hibernate + JPA + Spring Help!!!
By logixplayer in forum JDBCReplies: 0Last Post: 08-29-2009, 07:32 PM -
Java Application Architecture for Struts, Spring and Hibernate
By stujava in forum Advanced JavaReplies: 0Last Post: 08-20-2009, 07:39 AM -
Hibernate, Spring developers seeked at GBIF, Copenhagen
By m.doering in forum Jobs OfferedReplies: 0Last Post: 07-23-2008, 03:07 PM -
Lead Java developer -- Spring and Hibernate experience
By orjueladiego in forum Jobs OfferedReplies: 0Last Post: 10-29-2007, 01:50 PM -
Log4J, problem with Hibernate and Spring
By Marcus in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 03:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks