Issue
I got error in hibernate web application FileNotFoundException for which i have given right file path
Below is my error log
java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:86)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:123)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
at eql.com.dao.EmployeeDAO.addEmployee(EmployeeDAO.java:16)
at eql.com.controller.EmployeeControllerServlet.doPost(EmployeeControllerServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
Below is my method which will call in servlet
public void addEmployee(String FirstName,String LastName,String email,String Password,String phone, String City) {
try {
Configuration configuration = new Configuration().configure("src/main/resources/hibernate.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction =session.beginTransaction();
Employee em=new Employee();
em.setFirstname(FirstName);
em.setLastname(LastName);
em.setEmailid(email);
em.setPassword(Password);
em.setMobile(phone);
em.setCity(City);
session.save(em);
transaction.commit();
System.out.println("\n\n Details Added");
}
catch(HibernateException e) {
System.out.println(e.getMessage());
System.out.println("error");
}
Hibernate-configuration is as follow
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">sql123</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>
<mapping resource="src/main/resources/Employee-hbn.xml" />
</session-factory>
</hibernate-configuration>
POJO-Mapping is as follows:
<hibernate-mapping>
<class name="eql.com.model.Employee" table="Employee123">
<id column="ID" name="empid" type="java.lang.Integer" />
<property column="FirstName" name="firstname" type="java.lang.String" />
<property column="LastName" name="lastname" type="string" />
<property column="Email" name="emailid" type="java.lang.String" />
<property column="Password" name="password" type="java.lang.String" />
<property column="Phone" name="mobile" type="java.lang.String" />
<property column="City" name="city" type="java.lang.String" />
</class>
</hibernate-mapping>
Can anybody would have solution? help me out...
Solution
The main thing you should understand:
the path to the hibernate.cfg.xml
should be determined not using a source tree (like in the picture, provided by you), but by the some rules for class path resources.
Why the mapping.xml and configuration.xml has to be outside the pojo package?
So for your source tree:
Configuration configuration = new Configuration()
.configure("src/main/resources/hibernate.cfg.xml");
this should be changed too
Configuration configuration = new Configuration().configure();
and this
<mapping resource="src/main/resources/Employee-hbn.xml" />
should be
<mapping resource="Employee-hbn.xml" />
java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
You have this error because of you don't have a jar with javax.xml.bind.JAXBException
class in the class path. Other reasons can be: there are two jars with different versions, or jar is loaded by different classloaders (unlikely).
So the main reason is you specified incorrect paths to the resources and after that javax.xml.bind.JAXBException
is generated, but there is not the class for this exception in the class path. So the original error is hidden by java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
.
Answered By - v.ladynev
Answer Checked By - Clifford M. (JavaFixing Volunteer)