由 ClassLoadingException 和 ClassNotFoundException 引起的休眠 ServiceException
hibernate ServiceException caused by ClassLoadingException & ClassNotFoundException
我对使用 hibernate 还很陌生,我正在尝试创建一个以 hibernate 作为后端的网络应用程序,但我 运行 遇到了一个错误,我似乎无法弄清楚如何解决鉴于我的代码的当前配置。任何帮助将不胜感激
我已经将我的 hibernate.cfg.xml 配置添加到 hibernateutil java class 但我似乎无法让我的配置从我的数据库中读取数据并继续获取
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
由另一个错误引起:Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]
和 ClassNotFoundException
再次到 mysql 驱动程序
我以为我的 class 配置正确,但我似乎无法让它工作,这是我的错误开始的代码
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
settings.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3308/demo");
settings.put(Environment.USER, "user");
settings.put(Environment.PASS, "password");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
这是我的 DAO class
public class StudentDAO {
public void saveStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public void updateStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(student); //student object updated
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public Student getStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
student = session.get(Student.class, id); //get student object by id
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return student;
}
@SuppressWarnings("unchecked")
public List<Student> getAllStudents() {
Transaction transaction = null;
List<Student> students = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
students = session.createQuery("from student").list(); //get all student objects
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return students;
}
@SuppressWarnings("null")
public void deleteStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
student = session.get(Student.class, id);
if (student != null) {
session.delete(student);
System.out.println(student + "has been deleted");
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
}
这是我的 servlet class
@WebServlet("/")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 2L;
private StudentDAO studentDao;
String home = "/Week05";
public StudentServlet() {
this.studentDao = new StudentDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sPath = request.getServletPath();
//switch statement to call appropriate method
switch (sPath) {
case "/new":
try {
showNewForm(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
case "/insert":
try {
insertStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/delete":
try {
deleteStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/update":
try {
updateStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/edit":
try {
editStudent(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
default:
try {
listAllStudents(request, response); //home page = .../week04/StudentServlet
} catch (ServletException | IOException | SQLException e) {
e.printStackTrace();
}
break;
}
}
// functions to fetch data from studentDao and display data on appropriate jsp
private void listAllStudents(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
List<Student> allStudents = studentDao.getAllStudents();
request.setAttribute("listStudents", allStudents);
RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp"); //home page week04/StudentServlet | list all objects from table
dispatch.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp");
dispatch.forward(request, response);
}
private void insertStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student newStudent = new Student(firstname, lastname, email);
studentDao.saveStudent(newStudent); //student object inserted to table
response.sendRedirect(home); //redirect to home page
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
studentDao.deleteStudent(id); //student object deleted
response.sendRedirect(home);
}
private void updateStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
int id = Integer.parseInt(request.getParameter("id"));
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student updateStudent = new Student(id, firstname, lastname, email);
studentDao.updateStudent(updateStudent); //student object updated
response.sendRedirect(home);
}
private void editStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
// String firstname = request.getParameter("firstname");
// String lastname = request.getParameter("lastname");
// String email = request.getParameter("email");
Student currentStudent = studentDao.getStudent(id);
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp"); //student form called with current student info loaded
request.setAttribute("student", currentStudent);
dispatch.forward(request, response);
}
}
任何关于我出错的地方的帮助都将不胜感激,因为我真的不知道到底该怎么做,这可能是一个映射错误问题,但我假设所有必要的映射都包含在 servlet 中。
@Alpheus
您的项目无法找到您的驱动程序的依赖项,因为您没有将依赖项放在构建路径上。
我对使用 hibernate 还很陌生,我正在尝试创建一个以 hibernate 作为后端的网络应用程序,但我 运行 遇到了一个错误,我似乎无法弄清楚如何解决鉴于我的代码的当前配置。任何帮助将不胜感激
我已经将我的 hibernate.cfg.xml 配置添加到 hibernateutil java class 但我似乎无法让我的配置从我的数据库中读取数据并继续获取
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
由另一个错误引起:Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver]
和 ClassNotFoundException
再次到 mysql 驱动程序
我以为我的 class 配置正确,但我似乎无法让它工作,这是我的错误开始的代码
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
settings.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3308/demo");
settings.put(Environment.USER, "user");
settings.put(Environment.PASS, "password");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
这是我的 DAO class
public class StudentDAO {
public void saveStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public void updateStudent(Student student) {
Transaction transaction = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(student); //student object updated
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
public Student getStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
student = session.get(Student.class, id); //get student object by id
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return student;
}
@SuppressWarnings("unchecked")
public List<Student> getAllStudents() {
Transaction transaction = null;
List<Student> students = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
students = session.createQuery("from student").list(); //get all student objects
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
return students;
}
@SuppressWarnings("null")
public void deleteStudent(int id) {
Transaction transaction = null;
Student student = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
student = session.get(Student.class, id);
if (student != null) {
session.delete(student);
System.out.println(student + "has been deleted");
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
}
这是我的 servlet class
@WebServlet("/")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 2L;
private StudentDAO studentDao;
String home = "/Week05";
public StudentServlet() {
this.studentDao = new StudentDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sPath = request.getServletPath();
//switch statement to call appropriate method
switch (sPath) {
case "/new":
try {
showNewForm(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
case "/insert":
try {
insertStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/delete":
try {
deleteStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/update":
try {
updateStudent(request, response);
} catch (SQLException | IOException e) {
e.printStackTrace();
}
break;
case "/edit":
try {
editStudent(request, response);
} catch (ServletException | IOException e) {
e.printStackTrace();
}
break;
default:
try {
listAllStudents(request, response); //home page = .../week04/StudentServlet
} catch (ServletException | IOException | SQLException e) {
e.printStackTrace();
}
break;
}
}
// functions to fetch data from studentDao and display data on appropriate jsp
private void listAllStudents(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
List<Student> allStudents = studentDao.getAllStudents();
request.setAttribute("listStudents", allStudents);
RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp"); //home page week04/StudentServlet | list all objects from table
dispatch.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp");
dispatch.forward(request, response);
}
private void insertStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student newStudent = new Student(firstname, lastname, email);
studentDao.saveStudent(newStudent); //student object inserted to table
response.sendRedirect(home); //redirect to home page
}
private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
studentDao.deleteStudent(id); //student object deleted
response.sendRedirect(home);
}
private void updateStudent(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException{
int id = Integer.parseInt(request.getParameter("id"));
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
Student updateStudent = new Student(id, firstname, lastname, email);
studentDao.updateStudent(updateStudent); //student object updated
response.sendRedirect(home);
}
private void editStudent(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
// String firstname = request.getParameter("firstname");
// String lastname = request.getParameter("lastname");
// String email = request.getParameter("email");
Student currentStudent = studentDao.getStudent(id);
RequestDispatcher dispatch = request.getRequestDispatcher("student-form.jsp"); //student form called with current student info loaded
request.setAttribute("student", currentStudent);
dispatch.forward(request, response);
}
}
任何关于我出错的地方的帮助都将不胜感激,因为我真的不知道到底该怎么做,这可能是一个映射错误问题,但我假设所有必要的映射都包含在 servlet 中。
@Alpheus 您的项目无法找到您的驱动程序的依赖项,因为您没有将依赖项放在构建路径上。