0

i am creating a spring-mvc project in which i had used hibernate integration.In this i have 2 classes one is operationdao which is used to perform hibernate operation and other is my convtroller class.

Code of operationDao class:

public class OperationDao {
  Configuration cfg=null;
public OperationDao() {
  // TODO Auto-generated constructor stub

   cfg=new Configuration();
  cfg.configure("hibernate.cfg.xml");
}
public String saveShop(shops s)
{

  SessionFactory factory=cfg.buildSessionFactory();
  Session session=factory.openSession();
  org.hibernate.Transaction t=session.beginTransaction();
  try
  {
      Query query=session.createQuery("from shops");
      query.setCacheable(true);
      List<shops> list=query.list();
      Iterator<shops> i=list.iterator();
      while(i.hasNext())
      {
          shops shps=i.next();
          if(shps.getShops_string().equals(s.getShops_string()))
          {
              return "done-2";
          }
      }
      session.save(s);
      t.commit();
      return "done"+String.valueOf(s.getId());
  }
  catch(Exception e)
  {
      t.rollback();
      return "done-1";
  }
  finally
  {
      session.close();
  }

}
}

code in my Controller class:

public class EmpController {
  OperationDao dao;
  public EmpController() {
      // TODO Auto-generated constructor stub
      dao=new OperationDao();
  }
  @RequestMapping("/saveShop")
public void saveShop(@ModelAttribute("shop") shops shop,HttpServletResponse responce)
{
      String i=dao.saveShop(shop);
      try {
          responce.getWriter().println(i);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
}
}

But when i am trying to run my project on server,it shows me following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: work.OperationDao work.EmpController.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [work.OperationDao]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

But when i am creating instance of OpertionDao class for testing purpose than it doesn't show any error.so why error is coming in creating bean. Please Help i a badly stuck. Thanks in Advance:

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
yogesh meghnani
  • 729
  • 2
  • 6
  • 11
  • 1
    Have a look: https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java – Rana_S Jun 13 '17 at 19:35

1 Answers1

0

First of all, please check that you confiured spring context properly - you have decribed in spring configuration (xml or java based) your beans. It looks like you have not done it. Also, with Spring you don't need to instansiate you DAO via new DAO() construction. As a very good example you can find here http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial