Results 1 to 8 of 8

Thread: HibernateDaoSupport

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    Default HibernateDaoSupport

    this is my UserDAO,

    package org.innolab.entity;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;


    public class UserDAO extends HibernateDaoSupport {
    private static final Log log = LogFactory.getLog(UserDAO.class);


    protected void initDao() {
    //do nothing
    }

    public void save(User transientInstance) {
    log.debug("saving User instance");
    try {
    getHibernateTemplate().save(transientInstance);
    log.debug("save successful");
    } catch (RuntimeException re) {
    log.error("save failed", re);
    throw re;
    }
    }
    }


    and there is my applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
    </property>
    </bean>
    <bean id="UserDAO" class="org.innolab.entity.UserDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    </beans>

    and I got null pointer exception when I use my UserDAO, could anybody help me ? I am newbie thanks

  2. #2

    Default

    Hi,

    Can you attach the stack Trace of the error so that it would be easier to investigate and as well paste the code of your hibernate.cfg.xml file.

    Kartik

  3. #3
    Join Date
    Mar 2010
    Posts
    7

    Default

    this is my hibernate.cfg.xml
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <!-- Generated by MyEclipse Hibernate Tools. -->
    <hibernate-configuration>

    <session-factory>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.url">jdbc:mysql://222.18.63.38:3306/innolab</property>
    <property name="connection.username">root</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driv er</property>
    <property name="myeclipse.connection.profile">com.mysql.jdbc .Driver</property>

    <mapping resource="org/innolab/entity/User.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>



    and this is stack

    java.lang.NullPointerException
    org.innolab.action.LoginAction.execute(LoginAction .java:36)
    org.apache.struts.chain.commands.servlet.ExecuteAc tion.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAc tion.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase .execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(Ch ainBase.java:190)
    org.apache.commons.chain.generic.LookupCommand.exe cute(LookupCommand.java:304)
    org.apache.commons.chain.impl.ChainBase.execute(Ch ainBase.java:190)
    org.apache.struts.chain.ComposableRequestProcessor .process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doGet(Actio nServlet.java:449)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

    and this is my login action


    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    UserDAO userDAO = new UserDAO();
    userDAO.save(new User("10","10"));
    return mapping.findForward("result");
    }


    thanks

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Use [ code][/code ] tags when posting code.

    Yyou are creating a new instance in the Action yourself instead of using the one from the applicationContext.

    Code:
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    UserDAO userDAO = new UserDAO();
    userDAO.save(new User("10","10"));
    return mapping.findForward("result");
    }
    Next to that is is not recommended to use HibernateDaoSupport or HibernateTemplate anymore, simply use the Sessionfactory. Another note your catch is pretty much useless you shouldn't catch the exception, finally also saving/updating without proper transactionmanagent isn't going to work, unless you are using MyISAM tables which don't support transactions.

    Code:
    public class UserDAO {
    	private static final Log log = LogFactory.getLog(UserDAO.class);
    	private SessionFactory sf;
    	
    	public void setSessionFactory(SessionFactory sf) {
    		this.sf=sf;
    	}
    
    	public void save(User transientInstance) {
    		log.debug("saving User instance");
    		sf.getCurrentSession().save(transientInstance);
    	}
    }
    Last edited by Marten Deinum; Mar 10th, 2010 at 04:26 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2010
    Posts
    7

    Default

    thanks for the advice, but I've changed applicationContext.xml as followed

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation"
    			value="classpath:hibernate.cfg.xml">
    		</property>
    	</bean>
    	<bean id="UserDAO" class="org.innolab.entity.UserDAO">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="login" class="org.innolab.action.LoginAction">
    		<property name="userDAO" >
    			<ref bean="UserDAO"/>
    		</property>
    	</bean>
    	</beans>

    and here is my
    Code:
    package org.innolab.action;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.innolab.base.BaseAction;
    import org.innolab.entity.User;
    import org.innolab.entity.UserDAO;
    
    public class LoginAction extends Action implements BaseAction {
    	private UserDAO userDAO;
    
    	@Override
    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response)
    			throws Exception {
    		userDAO.save(new User("10", "10"));
    		return mapping.findForward("result");
    	}
    
    	public void setUserDAO(UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}
    
    }

    it's still giving me the null point exception...thanks

    Code:
    java.lang.NullPointerException
    	org.innolab.action.LoginAction.execute(LoginAction.java:22)
    	org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    	org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    	org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    	org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    	org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    	org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    	org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    	org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    	org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Actions aren't managed by spring they are managed by struts, I suggest the spring reference guide, especially the struts integration part.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Mar 2010
    Posts
    7

    Default

    thanks for the advice I will read that part of it...

  8. #8
    Join Date
    Mar 2010
    Posts
    7

    Default

    I've solved my proble, I've add a new line to my struts-config.xml


    Code:
    <controller>
    		<set-property property="processorClass"
    			value="org.springframework.web.struts.DelegatingRequestProcessor" />
    	</controller>
    and I've changed my applicationContext.xml to
    Code:
    <bean name="/login" class="org.innolab.action.LoginAction">
    		<property name="userDAO" >
    			<ref bean="UserDAO"/>
    		</property>
    	</bean>

Posting Permissions

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