In general, I get this error when trying to get data from the mySql database in the servlet.

@WebServlet(name = "PatientServlet", urlPatterns = "/patient") public class PatientServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Session session = HibernateUtill.getSessionFactory().openSession(); PatientDAOImpl patientDAO = new PatientDAOImpl(); patientDAO.setSession(session); try { request.setAttribute("patients", patientDAO.findAll()); } catch (Exception e) { e.printStackTrace(); } request.getRequestDispatcher("WEB-INF/patient.jsp").forward(request, response); } 

}

I use maven, all dependences are satisfied. If I try to connect to the database from a normal class, not a servlet, and just output some data from the database to the console, everything works.

 org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.likhanov.model.dao.entities.Patient"/> at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1597) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505) at org.hibernate.cfg.Configuration.configure(Configuration.java:1425) at org.hibernate.cfg.Configuration.configure(Configuration.java:1411) at com.likhanov.model.HibernateUtill.<clinit>(HibernateUtill.java:11) at com.likhanov.controllers.PatientServlet.doGet(PatientServlet.java:25) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) SEVERE: Servlet.service() for servlet [PatientServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause java.lang.ExceptionInInitializerError at com.likhanov.model.HibernateUtill.<clinit>(HibernateUtill.java:15) at com.likhanov.controllers.PatientServlet.doGet(PatientServlet.java:25) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 

Full repository code.

The error, by the way, points to the HibernateUtill class. I suspect that tomcat does not see the hibernate.cfg.xml file.

In the war file packed all available liby that pulled maven. I understand that maybe this is unnecessary, since the idea itself did not warn anything about it (the fix did not catch fire), but I thought at least that would work. Still threw a config. hibernate file

Just run and go to the right address. In theory, the servlet responsible for this address should retrieve the necessary data from the database, but it drops, and points to HibernateUtill. I apologize if anything, just started working with the web.

enter image description here

  • Look at line 14 of HibernateUtill . The code swallows all exceptions and displays them in the log. So ExceptionInInitializerError says little. Give entries for the previous exceptions. - default locale
  • Removed stack stack by mistake. Error log changed. The problem is specifically in the integration of tomcat and hibernate. Without tomcat, everything works. - Likhanov
  • here is the solution - Senior Pomidor
  • and also tell how you collect and deploy to the server. - Senior Pomidor
  • I would insert more pictures, but no more. - Likhanov

1 answer 1

I solved the problem. Replaced configuration () with AnnotationsConfiguration (), and also added an older dependency hibernate core.