Hibernate sessionFactory bean抛出java.lang.NullPointerException

时间:2018-11-10 18:28:16

标签: java spring hibernate

I am migrating jdbc to hibernate and i have palced below hibernate configuration in my application.

public class HibernateConfiguration {

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.cm.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(jdbcurl);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", true);
        properties.put("hibernate.format_sql", true);
        return properties;
    }


    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }

}

我的应用程序在应用程序启动时与数据库很好地交互,并且通过会话工厂也成功创建了休眠会话,并提供了输出。

**@Autowired
private SessionFactory sessionFactory;**

protected Session getSession() {
    return sessionFactory.getCurrentSession();
}

但是在应用程序启动后,当我通过控制器命中DAO时,会话工厂bean获得Null引用并由于无法创建或打开休眠会话而抛出NullPointerException,我试图找出解决方案,但是那不起作用,请让我知道上面的原因由于创建了问题,SessionFactory bean具有nullPointer。

只是为了测试我的DAO逻辑,我正在使用此控制器,并且此控制器命中了sessionFacory bean为null的DAO。

@RestController
        @RequestMapping("/Emp")
        public class myController {
          @RequestMapping(value = "/findByChannelManager", method = RequestMethod.GET)
            public void findemp() {

                HotelDaoImpl hotelDaoImpl=new HotelDaoImpl();
                List <HotelEntity> list = new ArrayList<>();
                list = hotelDaoImpl.findByChannelManager (EnumCM.AR);
                for (HotelEntity pro : list) {
                    System.out.println(pro);
            }
        }
    }


@Repository
@Transactional
public class HotelDaoImpl extends AbstractDao implements IHotelDao {

    @SuppressWarnings({ "unchecked", "unused" })
    @Override
    public List<HotelEntity> findByChannelManager(EnumCM cm) {
        List<HotelEntity> list = null;
        try {
        Session s = getSession();
        Criteria criteria=s.createCriteria(Hotel.class);
        criteria.add(Restrictions.eq("channelManager", "cm.name()"));
        list = criteria.list();
        }catch(Exception e) {
            LOGGER.debug("error " +e.getMessage());
            e.printStackTrace();

        }
        return list;
    }

public abstract class AbstractDao {

    @Autowired
    private SessionFactory sessionFactory;
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    }

1 个答案:

答案 0 :(得分:0)

您无法从控制器访问dao。您可以从服务访问dao,因此添加服务类。试试这个代码

@RestController
        @RequestMapping("/Emp")
        public class myController {
@Autowired
HotelService service;
          @RequestMapping(value = "/findByChannelManager", method = RequestMethod.GET)
            public void findemp() {


                List <HotelEntity> list = new ArrayList<>();
                list = service.findByChannelManager (EnumCM.AR);
                for (HotelEntity pro : list) {
                    System.out.println(pro);
            }
        }
    }
@Service
@Transactional
public class HotelService {
@Autowired
private HotelDao dao;

public List<HotelEntity> findByChannelManager(EnumCM cm) {
   return dao.findByChannelManager(EnumCM cm);
}
}

@Repository
public class HotelDaoImpl extends AbstractDao implements IHotelDao {

    @SuppressWarnings({ "unchecked", "unused" })
    @Override
    public List<HotelEntity> findByChannelManager(EnumCM cm) {
        List<HotelEntity> list = null;
        try {
        Session s = getSession();
        Criteria criteria=s.createCriteria(Hotel.class);
        criteria.add(Restrictions.eq("channelManager", "cm.name()"));
        list = criteria.list();
        }catch(Exception e) {
            LOGGER.debug("error " +e.getMessage());
            e.printStackTrace();

        }
        return list;
    }

public abstract class AbstractDao {

    @Autowired
    private SessionFactory sessionFactory;
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    }