I am trying to set up a simple Hibernate program setup. Below is my file structure in eclipse:
Students.java code (POJO)
package com.Hibernate;
public class Students {
private int id;
private String name;
public Students() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id =id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
Students.hbm.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name ="com.Hibernate.Students" table="students">
<id name="id" column="sid" type="int">
<generator class="assigned"/>
</id>
<property name="name" column="sname" type="string"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- mysql dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- database connection settings -->
<property name ="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://http://localhost/phpmyadmin/</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Mapping file entry -->
<mapping resource="Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java code:
package com.Hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
//create session factory from standard config file
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
}catch(Throwable ex) {
//log the exception
System.out.println("Initial Session Factory creation failed "+ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Client.java code:
package com.Hibernate;
import org.hibernate.*;
public class Client {
public static void main(String[] args) {
//create session object
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx= session.beginTransaction();
Students s1 = new Students();
s1.setId(33);
s1.setName("Assama");
session.save(s1);
tx.commit();
}catch(HibernateException e) {
e.printStackTrace();
}finally {
session.close();
}
}
}
I am getting an error in console as below:
Feb 11, 2023 4:37:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.1.Final}
Feb 11, 2023 4:37:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Initial Session Factory creation failed org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.Hibernate.HibernateUtil.<clinit>(HibernateUtil.java:24)
at com.Hibernate.Client.main(Client.java:7)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at com.Hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15)
... 1 more
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
... 10 more
How can I resolve these errors. I have tried specifying the pat of cfg.xml in configure(), but didn't work.