Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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();
}

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.

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();
    }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
    }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...