Hibernate sessionFactory bean抛出java.lang.NullPointerException [英] Hibernate sessionFactory bean throwing java.lang.NullPointerException
问题描述
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;
}
}
我的应用程序在应用程序启动时与数据库很好地交互,并且通过会话工厂也成功地创建了休眠会话,并提供了输出.
my application interacting fine with database at application startup creating hibernate session successfully through session factory giving output also.
**@Autowired
private SessionFactory sessionFactory;**
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
但是在应用程序启动后,当我通过控制器命中DAO时,会话工厂bean获取Null引用并由于无法创建或打开休眠会话而抛出NullPointerException,我试图找出解决方案,但这种方法不起作用,请让我知道上面的原因由于创建了问题,SessionFactory bean具有nullPointer.
but after application startup when i hitting DAO by controller then session factory bean getting Null reference and throwing NullPointerException due to which unable to create or open hibernate session , i tried to find out solution but that's not working please let me know why above SessionFactory bean having nullPointer due to which issue created.
只是为了测试我的DAO逻辑,我正在使用此控制器,并且此控制器命中了sessionFacory bean为null的DAO.
Just to test my DAO logic I am using this controller and This controller hitting to DAO where sessionFacory bean is null.
@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();
}
}
推荐答案
您无法从控制器访问dao.您可以从服务访问dao,因此添加服务类.试试这个代码
You cant access dao from your controller. You can access dao from service so add service class. Try this code
@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();
}
}
这篇关于Hibernate sessionFactory bean抛出java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!