Results 1 to 2 of 2
  1. #1
    pestilencia is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default 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
    Java 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>
    struts.xml
    Java 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>
    applicationContext-hibernate.xml
    Java 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-actions.xml
    Java 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-services.xml
    Java 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>
    spring-hibernate-daos.xml
    Java 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>
    FirstAction.java
    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
    Java 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;
    	}
    }
    FirstDAOImpl.java
    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 ;
    	}
    }
    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.

    Regards,
    Khosrow.

  2. #2
    kzvi.kzvi.1 is offline Member
    Join Date
    Oct 2008
    Location
    US
    Posts
    58
    Rep Power
    0

    Default

    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

  1. Replies: 0
    Last Post: 08-29-2009, 07:32 PM
  2. Replies: 0
    Last Post: 08-20-2009, 07:39 AM
  3. Hibernate, Spring developers seeked at GBIF, Copenhagen
    By m.doering in forum Jobs Offered
    Replies: 0
    Last Post: 07-23-2008, 03:07 PM
  4. Lead Java developer -- Spring and Hibernate experience
    By orjueladiego in forum Jobs Offered
    Replies: 0
    Last Post: 10-29-2007, 01:50 PM
  5. Log4J, problem with Hibernate and Spring
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •