引起:java.lang.ClassNotFoundException:无法加载请求的类:models.Category

时间:2016-06-14 11:04:28

标签: java hibernate

我正在尝试学习Hibernate框架hibernate-release-5.1.0.Final。我在lib\required, lib\jpa, und lib\java8中包含了所有必需的罐子。请看下面的屏幕截图,我为Student课程创建了一个数据库hb_student_tracker,但我收到Caused by: java.lang.ClassNotFoundException: Could not load requested class : models.Category下面的错误 我正在使用XAMPP控制面板的默认配置。用户名也是root,没有密码。

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>

学生

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",
                    "paul.wall@gmail.com");

            // 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();
        }

    }

}

错误

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

enter image description here

5 个答案:

答案 0 :(得分:6)

问题出在hibernate.cfg.cml

<mapping class ="models.Category" />

你想在这里给出带注释的模型类,即

<mapping class ="com.tutorial.hibernate.demo.entity.Student" /> 

并且您应该继续在这里添加新的模型类以进行ORM映射。

答案 1 :(得分:0)

我认为你在映射中传递了错误的实体类

<mapping class ="models.Category" />

你应该通过

<mapping class ="package in which student entity define.Student" />

答案 2 :(得分:0)

您的hibernate.cfg.xml文件中有错误。您的应用程序中没有Category类,因此hibernate映射<mapping class ="models.Category" />不应该处于休眠配置中。这就是为什么hibernate给出错误无法加载请求的类:models.Category

您应该映射Student类,而不是Category。

<mapping class = "com.tutorial.hibernate.demo.entity.Student" />

答案 3 :(得分:0)

在hibernate.cfg.xml配置文件中指向错误的包。

尝试映射正确的映射类。

答案 4 :(得分:0)

在执行休眠模式时,我也遇到了同样的问题。 但是现在我有了解决方案,我将所有休眠的jar文件放在类路径中。 右键单击您的项目>>转到构建路径>>选择类路径>>单击外部jar(将jar文件添加到任何位置)。