Announcement Announcement Module
Collapse
No announcement yet.
Hibernate reutrning null in call from controller. problem with annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate reutrning null in call from controller. problem with annotations

    Hi I am using annotations in my dispatcher and controller. but when i make a call to hibernate it is returning null from my DAO object. can you please help. I feel something wrong with the annotations i have placed in the file. help me to locate and rectify the problem

    here is my code in dispatcher servlet for hibernate
    Code:
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
    <list>
    <value>customer.customer</value>
    <value>customer.categories</value>
    <value>geography.countries</value>
    <value>geography.states</value>
    
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.connection.autocommit">true</prop>
    </props>
    </property>
    </bean>
     
    <bean id="mycustomerDAO" class="customer.customerDAOImpl">
    <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    Here is my DAO implementation code
    Code:
    import geography.*;
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.bind.annotation.ModelAttribute;
    
    @Repository("mycustomerDAO")
    public class customerDAOImpl implements customerDAO {
     
    private HibernateTemplate hibernateTemplate;
    
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory)
    {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
     
    @Override
    public void savecustomer(customer customer) {
    hibernateTemplate.saveOrUpdate(customer);
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<customer> listcustomer() {
    return hibernateTemplate.find("from customer");
    }
    
    @Override
    @Transactional
    public List<countries> listCountries() {
    return hibernateTemplate.find("from countries order by name");
    }
    
    @Override
    
    public List<categories> listCategories() {
    return hibernateTemplate.find("from customer_category order by name");
    }
    
    @Override
    
    public List<states> listStates(int id) {
    return hibernateTemplate.find("from states where country_id="+id+" order by name");
    }
    
    }
    Here is the code i am using in controller to call this DAO implementation listCountries method
    Code:
    @RequestMapping("/customer.htm")
       public ModelAndView customer(HttpServletRequest request,HttpServletResponse response) throws Exception
        {
            ModelMap modelMap = new ModelMap();
            modelMap.addAttribute("customer", new customer());
            modelMap.addAttribute("countries", new countries());
            modelMap.addAttribute("countryList", customerDAO.listCountries());
            return new ModelAndView("customer",modelMap);
        }
    it is throwing null pointer exception

    thanks
    david

  • #2
    I have fixed this problem. just changed the

    private HibernateTemplate hibernateTemplate;

    to
    public HibernateTemplate hibernateTemplate;

    this was possible after reading some old threads on hibernate in this forum. thanks to all those authors for help

    Comment

    Working...
    X