首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >起因: org.o7planning.impl.EmployeDAOImpl.<init>(EmployeDAOImpl.java:34)的java.lang.NullPointerException

起因: org.o7planning.impl.EmployeDAOImpl.<init>(EmployeDAOImpl.java:34)的java.lang.NullPointerException
EN

Stack Overflow用户
提问于 2017-02-11 03:18:33
回答 1查看 109关注 0票数 1

这是我尝试实例化bean的file.XML : applicationDaoContext.XML

代码语言:javascript
复制
`
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

 <bean id="EmployeDAO" class="org.o7planning.impl.EmployeDAOImpl">
     <property name="sessionFactory" ref="SessionFactory" />
 </bean>
</beans>  `

这是我的类EmployeDaooImpl,实例化失败:( hibernate spring maven tomcat server ) with eclipse

代码语言:javascript
复制
package org.o7planning.impl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.o7planning.dao.EmployeDao;
import org.o7planning.Entity.Employe;
import org.hibernate.Query;

import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class EmployeDAOImpl implements EmployeDao {

    public EmployeDAOImpl() {
        super();


    }

        public void init(){
            new EmployeDAOImpl();
            System.out.println(" mossab method initi de la classe employimpldao");
        }
    private SessionFactory sessionFactory;

    Session session = sessionFactory.getCurrentSession();

            // methode implementer par l'interface
            @SuppressWarnings("unchecked")
            public List<Employe> getAllEmploye() {

                Session session = this.sessionFactory.getCurrentSession();

                List<Employe> list = session.createQuery("from EMPLOYEES").getResultList()  ;
                session.close();
                return list;
}
                // method implementer par l'interface   
            public Integer getMaxEmployeByID() {
                Session session = this.sessionFactory.getCurrentSession();
                String sql = "Select max(E.EMPLOYEE_ID) from EMPLOYEES E ";
                Query query = session.createQuery(sql);
                Integer maxEmployId = (Integer) query.uniqueResult();
                  if (maxEmployId == null) {
                      return 0;
                  }

                  return maxEmployId;
            }

                // methode implementer par l'interface
            public void createEmploy(String nom, String prenom) {

          Integer employId = getMaxEmployeByID() + 1;
          Employe employ = new Employe();
          employ.setId_Employe(employId);
          employ.setNom(nom);
          employ.setPrenom(prenom);
          Session session = this.sessionFactory.getCurrentSession();
          session.persist(employ);

    }

            public SessionFactory getSessionFactory() {
                return sessionFactory;
            }

        @Autowired  
        public void setSessionFactory(SessionFactory sessionFactory) {
                  this.sessionFactory = sessionFactory;
              }

}
EN

回答 1

Stack Overflow用户

发布于 2017-05-06 00:55:34

您需要更改代码,如下所示:

代码语言:javascript
复制
private SessionFactory sessionFactory;
// Remove this line:
// Session session = sessionFactory.getCurrentSession();

此时,sessionFactory没有被赋值,所以它是null。当Spring向其注入值时,它的值不为空。Spring将调用setSessionFactory方法向Spring字段注入值。

你可以用setSessionFactory方法写更多的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42167058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档