Reputation: 2380
I am trying to learn the Hibernate framework hibernate-release-5.1.0.Final
. I have included all required jars in the lib\required, lib\jpa, und lib\java8
. Please see the screen shoot underneath, I created a database hb_student_tracker
for the Student class but I am getting the error below Caused by: java.lang.ClassNotFoundException: Could not load requested class : models.Category
I am using the default configuration of the XAMPP control panel. Also username is root
and no password.
hibernate.cfg.cml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">validate</property>
<mapping class ="models.Category" />
</session-factory>
</hibernate-configuration>
Student
package com.tutorial.hibernate.demo.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="id")
private int id;
@Column(name="first_name")
private String first_name;
@Column(name="last_name")
private String last_name;
@Column(name="email")
private String email;
public Student(){
}
public Student(String first_name, String last_name, String email) {
super();
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", first_name=" + first_name
+ ", last_name=" + last_name + ", email=" + email + "]";
}
}
CreateStudentDemo
package com.tutorial.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.demo.entity.Student;
public class CreateStudentDemo {
public static void main(String[] args) {
// create session factory.
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class).buildSessionFactory();
// create a session.
Session session = factory.getCurrentSession();
try {
// Use the session object to save Java object.
System.out.println("Creating a new Student object");
Student tempStundent = new Student("Paul", "Wall",
"[email protected]");
// start a transaction.
session.beginTransaction();
// save the student object.
System.out.println("Saving the student ...");
session.save(tempStundent);
// commit transaction.
session.getTransaction().commit();
System.out.println("Done ....");
} finally {
factory.close();
}
}
}
error
Jun 14, 2016 12:45:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Jun 14, 2016 12:45:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 14, 2016 12:45:09 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 14, 2016 12:45:09 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jun 14, 2016 12:45:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jun 14, 2016 12:45:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hb_student_tracker]
Jun 14, 2016 12:45:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jun 14, 2016 12:45:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 14, 2016 12:45:10 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Jun 14, 2016 12:45:10 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [models.Category]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:229)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:103)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.tutorial.hibernate.demo.CreateStudentDemo.main(CreateStudentDemo.java:15)
Caused by: java.lang.ClassNotFoundException: Could not load requested class : models.Category
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226)
... 9 more
Upvotes: 0
Views: 33693
Reputation: 1
I also faced the same problem while executing the hibernate. But now i got solution i put all my hibernate jar file in class path. Right click on your project >> go to build path >> select class path >> click on external jar (add your jar file wherever it is located ).
Upvotes: 0
Reputation: 138
You have an error in your hibernate.cfg.xml
file. You do not have the Category class in you application, so the hibernate mapping <mapping class ="models.Category" />
should not be in the hibernate configuration. That is why hibernate is giving the error Could not load requested class : models.Category
Instead of Category you should map the Student class i.e.
<mapping class = "com.tutorial.hibernate.demo.entity.Student" />
Upvotes: 1
Reputation: 1567
I think you passing the wrong entity class in mapping
<mapping class ="models.Category" />
You should pass the
<mapping class ="package in which student entity define.Student" />
Upvotes: 1
Reputation: 2200
The problem is in hibernate.cfg.cml
<mapping class ="models.Category" />
You are suppose to give annotated model class here ie.
<mapping class ="com.tutorial.hibernate.demo.entity.Student" />
and you should keep adding your new model classes here for ORM mapping.
Upvotes: 9
Reputation: 11
In hibernate.cfg.xml configuration file pointing wrong package.
Try to map the correct mapping class.
Upvotes: 0