title: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined"}}
body: <p>I'm having problem with my web-app, cannot deploy the project to WildFly.</p>
  
  <p>Problem:</p>
  
  <pre><code>Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookedRoomDao': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
      ... 3 more
  </code></pre>
  
  <p>Here is my application-context.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"&gt;
  
      &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; 
          &lt;property name="basenames"&gt; 
              &lt;list&gt; 
                  &lt;value&gt;text_resources&lt;/value&gt; 
              &lt;/list&gt; 
          &lt;/property&gt; 
      &lt;/bean&gt;
  
      &lt;bean name="exceptionResolver" class="com.booking.common.HotelException" autowire="byName"&gt;
          &lt;property name="exceptionMappings"&gt;
              &lt;props&gt;
                  &lt;prop key="javax.servlet.jsp.JspException"&gt;exception&lt;/prop&gt;
                  &lt;prop key="javax.servlet.ServletException"&gt;exception&lt;/prop&gt;
                  &lt;prop key="java.lang.Exception"&gt;exception&lt;/prop&gt;
                  &lt;prop key="java.lang.Throwable"&gt;exception&lt;/prop&gt;
                  &lt;prop key="java.lang.RuntimeException"&gt;exception&lt;/prop&gt;
                  &lt;prop key="org.springframework.web.bind.ServletRequestBindingException"&gt;exception&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
          &lt;property name="jndiName" value="java:jboss/mail/Default"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"&gt;
          &lt;property name="driverClass" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/booking-war"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value="root"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="true" autowire="default"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="com.booking.member.vo" /&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;    
  
      &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  
      &lt;context:component-scan base-package="com.booking.dao" /&gt;
          &lt;context:component-scan base-package="com.booking.member" /&gt;
      &lt;context:component-scan base-package="com.booking.service" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>UserDaoImpl.java</p>
  
  <pre><code>package com.booking.dao.impl;
  
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import com.booking.dao.GenericDao;
  import com.booking.dao.UserDao;
  import com.booking.member.vo.AuthenticateUser;
  
  @Repository("userDao")
  public class UserDaoImpl implements UserDao{
  
      @Autowired
      GenericDao&lt;AuthenticateUser,Long&gt; genericDao;
  
      @PersistenceContext
      EntityManager entityManager;
  
          @Override
      public List&lt;AuthenticateUser&gt; userByName(String username) {
          String queryStr = "SELECT u FROM AuthenticateUser u WHERE u.username= ? ";
          return entityManager.createQuery(queryStr)
                 .setParameter(1,username).getResultList();
      }
  
      public AuthenticateUser create(AuthenticateUser t) {
          return genericDao.create(t);
      }
  
      public AuthenticateUser read(Long id, Class&lt;AuthenticateUser&gt; c) {
          return genericDao.read(id, c);
      }
  
      public AuthenticateUser update(AuthenticateUser t) {
          return genericDao.update(t);
      }
  
      public void delete(AuthenticateUser t) {
          genericDao.delete(t);
      }
  
      public List&lt;AuthenticateUser&gt; getAll(Class&lt;AuthenticateUser&gt; c) {
          return genericDao.getAll(c);
      }
  }
  </code></pre>
  
  <p>GenericDaoImpl.java : </p>
  
  <pre><code>package com.booking.dao.impl;
  
  import java.io.Serializable;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.hibernate.Session;
  import org.hibernate.criterion.Example;
  import org.hibernate.ejb.HibernateEntityManager;
  import org.springframework.stereotype.Repository;
  
  import com.booking.dao.GenericDao;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  
  
  @Repository("genericDao")
  public class GenericDaoImpl&lt;T,PK extends Serializable&gt; implements GenericDao&lt;T, PK&gt; {
  
  
      protected Class&lt;T&gt; entityClass;
  
      @PersistenceContext
      protected EntityManager entityManager;
  
      @Override
      public T create(T t) {
          this.entityManager.persist(t);
          return t;
      }
  
      @Override
      public T read(PK id,Class&lt;T&gt; c) {
          return (T)this.entityManager.find(c, id);
      }
  
      @Override
      public T update(T t) {
           return this.entityManager.merge(t);
      }
  
      @Override
      public void delete(T t) {
         t = this.entityManager.merge(t);
         this.entityManager.remove(t);
      }
  
      @Override
      public List&lt;T&gt; getAll(Class&lt;T&gt; c){
          return this.entityManager.createQuery("SELECT o FROM "+ c.getName() +" o").getResultList();
      }
  
      @Override
      public List&lt;T&gt; getByFilter(T filter){
          Session session = ((HibernateEntityManager)entityManager.unwrap(HibernateEntityManager.class)).getSession();
          return session.createCriteria(filter.getClass()).add(Example.create(filter)).list();
      }
  
  }
  </code></pre>
  
  <p>BookedRoomDaoImpl.java : </p>
  
  <pre><code>package com.booking.dao.impl;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.NoResultException;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.beans.factory.annotation.Autowired;
  
  import org.springframework.stereotype.Repository;
  
  import com.booking.common.CommonUtils;
  import com.booking.dao.BookedRoomDao;
  import com.booking.dao.GenericDao;
  import com.booking.member.vo.BookedRoom;
  import com.booking.member.vo.Booking;
  import com.booking.member.vo.TimeRange;
  import org.hibernate.criterion.Restrictions;
  
  
  @Repository("bookedRoomDao")
  public class BookedRoomDaoImpl implements BookedRoomDao{
  
          @PersistenceContext
      EntityManager entityManager;
  
      @Autowired
      GenericDao&lt;TimeRange,Long&gt; timeRangeDao;
  
          @Override
      public List&lt;BookedRoom&gt; getBookedRooms(long id,String date){
          List&lt;BookedRoom&gt; list = new ArrayList&lt;BookedRoom&gt;();
          List&lt;TimeRange&gt; timeRanges = timeRangeDao.getAll(TimeRange.class);
          for(TimeRange timeRange:timeRanges){
              BookedRoom bookedRoom = new BookedRoom();
              Booking booking = null;
              try{
                  booking = (Booking)entityManager.createNativeQuery("select * from Booking b where b.time_range = ?1 and b.room = ?2 and date_format(date_booked,'%Y-%m-%d') = ?3",Booking.class)
                  .setParameter(1,timeRange.getId())
                  .setParameter(2,id)
                  .setParameter(3,date)
                  .getSingleResult();
  
              }catch (NoResultException e) {
  
              }
  
              if(booking!=null){
                  bookedRoom.setBookingId(booking.getBookingId());
                  bookedRoom.setDateBooked(booking.getDateBooked());
                  bookedRoom.setRoomName(booking.getRoom().getRoomName());
                  bookedRoom.setUserName(booking.getUserid().getUsername());
                  if(booking.getUserid().getId()==CommonUtils.getCurrentUserProfile().getUserId()){
                      bookedRoom.setOwner(true);
                  }
              }
  
              bookedRoom.setFromTime(timeRange.getFromTime());
              bookedRoom.setToTime(timeRange.getToTime());
              bookedRoom.setTrId(timeRange.getId());
              list.add(bookedRoom);
          }
          return list;
      }
          @Override
      public void deleteBooking(long bookingId,long userId){
          entityManager.createNativeQuery("delete from booking where booking_id=?1 and userid=?2")
          .setParameter(1, bookingId)
          .setParameter(2,userId)
          .executeUpdate();
      }
          @Override
      public List getBookingByRoomId(long roomId){
          return entityManager.createNativeQuery("select * from booking where room = ?1",Booking.class)
          .setParameter(1, roomId)
          .getResultList();
      }
  
  }
  </code></pre>
  
  <p>And hibernate.cfg.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;validation-mode&gt;NONE&lt;/validation-mode&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;
              &lt;!--value='create' to build a new database on each run; value='update' to modify an existing database; value='create-drop' means the same as 'create' but also drops tables when Hibernate closes; value='validate' makes no changes to the database--&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
              &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="hibernate.ejb.cfgfile" value="/resources/hibernate/hibernate.cfg.xml"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>