org.hibernate.MappingException:java.lang.ClassNotFoundException:Employee

时间:2014-12-11 06:32:49

标签: java hibernate

我是hibernate和使用hibernate版本4.3.4 JAR文件的新手。我正在尝试使用IntelliJ社区版中的mySQL服务器执行简单的Hibernate程序。以下是我的文件。

POJO档案

package TestPackage;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;

public Employee() {}
public Employee(String fname, String lname, int salary) {
    this.firstName = fname;
    this.lastName = lname;
    this.salary = salary;
}
public int getId() {
    return id;
}
public void setId( int id ) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName( String first_name ) {
    this.firstName = first_name;
}
public String getLastName() {
    return lastName;
}
public void setLastName( String last_name ) {
    this.lastName = last_name;
}
public int getSalary() {
    return salary;
}
public void setSalary( int salary ) {
    this.salary = salary;
}
}

映射文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
    <meta attribute="class-description">
        This class contains the employee detail.
    </meta>
    <id name="id" type="int" column="id">
        <generator class="native"/>
    </id>
    <property name="firstName" column="first_name" type="string"/>
    <property name="lastName" column="last_name" type="string"/>
    <property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>

Hibernate配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver

    </property>
    <!-- Related to the connection END -->
    <property name="connection.url">jdbc:sqlserver://v-dev-        sql:1433;database=SIMSevals;integratedSecurity=true</property>
    <property name="connection.user">deptisg</property>
    <property name="connection.password">IsgDev1</property>

    <!-- Related to hibernate properties START -->
    <property name="show_sql">true</property>
    <property name="dialet">org.hibernate.dialect</property>
    <property name="hbm2ddl.auto">update</property>
    <!-- Related to hibernate properties END -->

    <!-- Related to mapping START -->
    <mapping resource="testpackage/Employee.hbm.xml"/>
    <!-- Related to the mapping END -->

</session-factory>
</hibernate-configuration>

主类

 package TestPackage;

    import java.sql.*;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;

    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    //import org.hibernate.service.ServiceRegistryBuilder;`enter code here`


public class hibernate {
//private static SessionFactory sessionFactory;
//private static ServiceRegistry serviceRegistry;`enter code here`

public static void main(String[] args) throws SQLException, ClassNotFoundException  {

    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        //Connection conn = DriverManager.getConnection("jdbc:sqlserver://okeechobee.synygy.net:1433;database=SIMSNotes;integratedSecurity=true");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://v-dev-sql:1433;database=SIMSevals;integratedSecurity=true");

        //Configuration conf = new Configuration().configure("hibernate.cfg.xml");


        Configuration conf = new Configuration().configure("hibernate.cfg.xml");

        System.out.println("test34534543");
        ServiceRegistry sr = new   StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();


        SessionFactory sf = conf.buildSessionFactory(sr);

        Session session = sf.openSession();

        session.beginTransaction();


        //Session session = sf.openSession();
        session.beginTransaction();
        //Session session = sessionFactory.getCurrentSession();

        org.hibernate.Transaction tr = session.beginTransaction();

        Transaction tx = session.beginTransaction();

        Integer employeeID = null;


        conn.close();

    }catch (Exception e)
    {
        System.out.println(e);
    }
}
}

堆栈跟踪

    "C:\Program Files\Java\jdk1.7.0\bin\java" -Didea.launcher.port=7546 "-                              Didea.launcher.bin.path=C:\Users\vazek\AppData\Roaming\JetBrains\IntelliJ IDEA Community Edition                14.0.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0\jre\lib\charsets.jar;C:    \Program         Files\Java\jdk1.7.0\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0\jre\lib  \javaws.jar;C:\Program Files\Java    \jdk1.7.0\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0\jre\lib   \jsse.jar;C:\Program Files\Java\jdk1.7.0\jre    \lib\management-agent.jar;C:\Program Files\Java \jdk1.7.0\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0\jre    \lib\resources.jar;C:\Program Files  \Java\jdk1.7.0\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext    \antlr-2.7.7.jar;C:\Program     Files\Java\jdk1.7.0\jre\lib\ext\avro-1.7.6.jar;C:\Program Files\Java\jdk1.7.0\jre    \lib\ext\commons-  codec-1.6.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\commons-compress-1.5.jar;C:\Program     Files  \Java\jdk1.7.0\jre\lib\ext\commons-io-2.1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\commons-lang-      2.6.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext \dom4j- 1.6.1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\hibernate-commons-annotations- 4.0.5.Final.jar;C:  \Program Files\Java\jdk1.7.0\jre\lib\ext\hibernate-core-4.3.6.Final.jar;C:\Program  Files\Java\jdk1.7.0\jre \lib\ext\hibernate-entitymanager-4.3.6.Final.jar;C:\Program Files\Java  \jdk1.7.0\jre\lib\ext\hibernate-jpa-    2.1-api-1.0.0.Final.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext  \hibernate-search-analyzers-    4.5.2.Final.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\hibernate-   search-infinispan-4.5.2.Final.jar;C:    \Program Files\Java\jdk1.7.0\jre\lib\ext\infinispan-commons-    6.0.2.Final.jar;C:\Program Files\Java   \jdk1.7.0\jre\lib\ext\infinispan-core-6.0.2.Final.jar;C:\Program    Files\Java\jdk1.7.0\jre\lib\ext \infinispan-lucene-directory-6.0.2.Final.jar;C:\Program Files\Java  \jdk1.7.0\jre\lib\ext\jackson-core-asl- 1.9.13.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\jackson-  mapper-asl-1.9.13.jar;C:\Program Files\Java \jdk1.7.0\jre\lib\ext\jandex-1.1.0.Final.jar;C:\Program     Files\Java\jdk1.7.0\jre\lib\ext\javassist-3.18.1-   GA.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext   \jboss-logging-3.1.4.GA.jar;C:\Program Files\Java   \jdk1.7.0\jre\lib\ext\jboss-logging-annotations-    1.2.0.Beta1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext  \jboss-marshalling-1.4.4.Final.jar;C:   \Program Files\Java\jdk1.7.0\jre\lib\ext\jboss-marshalling-river-   1.4.4.Final.jar;C:\Program Files    \Java\jdk1.7.0\jre\lib\ext\jboss-transaction-api_1.1_spec-  1.0.1.Final.jar;C:\Program Files\Java   \jdk1.7.0\jre\lib\ext\jgroups-3.4.3.Final.jar;C:\Program Files\Java \jdk1.7.0\jre\lib\ext\jms-  1.1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\jsr250-api-1.0.jar;C:\Program    Files\Java  \jdk1.7.0\jre\lib\ext\jta-1.1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\localedata.jar;C:  \Program    Files\Java\jdk1.7.0\jre\lib\ext\lucene-analyzers-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib       \ext\lucene-core-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-grouping-3.6.2.jar;C:\Program      Files\Java\jdk1.7.0\jre\lib\ext\lucene-highlighter-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext     \lucene-kuromoji-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-memory-3.6.2.jar;C:\Program        Files\Java\jdk1.7.0\jre\lib\ext\lucene-misc-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-        phonetic-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-smartcn-3.6.2.jar;C:\Program Files     \Java\jdk1.7.0\jre\lib\ext\lucene-spatial-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-      spellchecker-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\lucene-stempel-3.6.2.jar;C:\Program       Files\Java\jdk1.7.0\jre\lib\ext\slf4j-api-1.6.1.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\solr-        analysis-extras-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\solr-core-3.6.2.jar;C:\Program Files       \Java\jdk1.7.0\jre\lib\ext\solr-solrj-3.6.2.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sqljdbc4.jar;C:      \Program Files\Java\jdk1.7.0\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext       \sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java     \jdk1.7.0\jre\lib\ext\xml-apis-1.3.03.jar;C:\Program Files\Java\jdk1.7.0\jre\lib\ext\zipfs.jar;C:\Users     \vazek\Desktop\Hibernate2\out\production\Hibernate2;C:\Users\vazek\AppData\Roaming\JetBrains\IntelliJ IDEA      Community Edition 14.0.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain         TestPackage.hibernate
hello 22ed
test1
Dec 11, 2014 11:40:38 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Dec 11, 2014 11:40:38 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties:      {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false,       hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true,      hibernate.generate_statistics=true, hibernate.connection.username=sa,       hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE,       hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true,        hibernate.connection.pool_size=5}
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Dec 11, 2014 11:40:38 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace       http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: testpackage/Employee.hbm.xml
test34534543
Dec 11, 2014 11:40:38 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
test21
Dec 11, 2014 11:40:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Dec 11, 2014 11:40:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://v-        dev-sql:1433;database=SIMSevals;integratedSecurity=true]
Dec 11, 2014 11:40:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator
INFO: HHH000046: Connection properties: {user=deptisg, password=****}
Dec 11, 2014 11:40:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     buildCreator
INFO: HHH000006: Autocommit mode: false
Dec 11, 2014 11:40:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl     configure
INFO: HHH000115: Hibernate connection pool size: 5 (min=1)
Dec 11, 2014 11:40:38 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Dec 11, 2014 11:40:38 AM org.hibernate.dialect.H2Dialect <init>
WARN: HHH000431: Unable to determine H2 database version, certain features may not work
Dec 11, 2014 11:40:39 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator      initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Dec 11, 2014 11:40:39 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
org.hibernate.MappingException: java.lang.ClassNotFoundException: Employee

请帮忙。

3 个答案:

答案 0 :(得分:2)

在你的hibernate映射文件和check.use中使用完整的类路径: class name="TestPackage.Employee"

<class name="TestPackage.Employee" table="EMPLOYEE">

答案 1 :(得分:1)

您需要添加 Hibernate配置文件中的<context:component-scan base-package="TestPackage"/>

答案 2 :(得分:0)

映射文件中的以下行应该从

更改
<class name="Employee" table="EMPLOYEE">

<class name="TestPackage.Employee" table="EMPLOYEE">

应在hibernate映射文件中指定实体的完整路径。