我尝试使用hibernate和wildfly-10.1.0.Final
实现泛型DaoBaseDao.java
BaseDao<T, PK extends Serializable> implements BaseDaoI<T, PK>
@Override
public T save(T t) {
try {
this.entityManager.persist(t);
return t;
} catch (PersistenceException pe) {
return null;
}
}
StudentDao.java
public class StudentDao extends BaseDao<Student, Long>{
public StudentDao(EntityManager entityManager) {
super(Student.class, entityManager);
}}
StudentBean.java
@Stateless
public class StudentBean implements StudentBeanLocal {
@PersistenceContext(name = "com.erick_SchoolManagementSystem_war_1.0-SNAPSHOTPU")
EntityManager em;
StudentDao stud = new StudentDao(em);
@Override
public Student add(Student student) {
return stud.save(student);
}
RegisterStudent.java
@EJB
StudentBeanLocal studentbean;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Student student = new Student();
student.setFirst_name(request.getParameter("first_name"));
student.setLast_name(request.getParameter("last_name"));
student.setUsername(request.getParameter("user_name"));
student.setPassword(request.getParameter("password"));
if(studentbean.add(student)!=null){
response.sendRedirect("Login.jsp");
}else{
response.sendRedirect("Register.jsp");
}
当我填写表格进行注册和提交时,我收到以下错误:
引起:java.lang.NullPointerException 在com.erick.schoolmanagementsystem.dao.BaseDao.save(BaseDao.java:34)