不知道出什么问题了。我得到的例外:


  org.hibernate.MappingException:未知实体:org.hibernate.employee


如何解决?我创建了client.java。我创建了与客户端匹配的映射表。我已经创建了映射并添加了映射

hibernateutil.java编码

package org.hibernate.test.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class hibernateutil {
private static SessionFactory sessionfactory;

public static SessionFactory getSessionFactory(){
    if(sessionfactory == null){
        synchronized(hibernateutil.class){
            if(sessionfactory == null){
                try {
                    Configuration conf=new Configuration().configure();
                        StandardServiceRegistryBuilder builder= new StandardServiceRegistryBuilder()
                            .applySettings(conf.getProperties());
                    sessionfactory = conf.buildSessionFactory(builder.build());

                }catch (Throwable th){
                    th.printStackTrace();
                    throw new ExceptionInInitializerError();
                }
            }
            return sessionfactory;
        }
    }

    else{
        return sessionfactory;
    }
}
}


employee.java

package org.hibernate;
public class employee {

private int id;
private String firstname;
private String lastname;
private int salary;

public employee(){}

public employee(int id,String firstname,String lastname,int salary)
{
    this.id=id;
    this.firstname=firstname;
    this.lastname=lastname;
    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 firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

}


hibernate.cfg.xml

      <?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>
             <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
               <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
             <property name="hibernate.connection.username">javaguy</property>
             <property name="connection.password"></property>
               <property name="hibernate.jdbc.batch_size">100</property>
                <property name="connection.pool_size">1</property>
                <property name="show_sql">false</property>
                <property name="format_sql">true</property>
                <property name="hibernate.hbm2ddl.auto">create</property>
                <mapping resource="org/hibernate/employee.hbm.xml"/>
        </session-factory>
        </hibernate-configuration>


employee.hbm.xml

        <?xml version="1.0"?>
         <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping       DTD 3.0//EN"
           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
         <!-- Generated Oct 20, 2016 7:56:20 PM by Hibernate Tools 3.5.0.Final -->
         <hibernate-mapping>
            <class name="employee" table="EMPLOYEE">
               <id name="id" type="int">
                    <column name="EMP_ID" />
                    <generator class="assigned" />
                 </id>
                <property name="firstname" type="java.lang.String">
                    <column name="FIRST_NAME" />
                </property>
                <property name="lastname" type="java.lang.String">
                   <column name="LAST_NAME" />
                 </property>
                <property name="salary" type="int">
                    <column name="SALARY" />
                </property>
             </class>
             </hibernate-mapping>


客户端程序

package learn;

import org.hibernate.Session;
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.test.util.hibernateutil;
import org.hibernate.*;


 public class client {

 private static SessionFactory factory;

public static void main(String[] args) {

    factory = hibernateutil.getSessionFactory();

    client a = new client();
    // Add few employee records in data base
    a.addemployee(1,"Sai","Nathan",5000);
    a.addemployee(2,"Sai","kumar",6000);
    a.addemployee(3,"Santhosh","Kumar",9000);

    // List down all employees
    /*a.listemployee();

    // update employee's record
    a.updateemployee(1,20000);

    // Delete an employee
    a.deleteemployee(3);

    // List down all employees
    a.listemployee();*/
    System.out.println("Finished");
}

   /* Method to CREATE an employee in the database */

public Integer addemployee(int id, String fname,String lname,int salary)
{
    Session session= factory.openSession();
    Transaction tx=session.beginTransaction();
    employee e1= new employee(id,fname,lname,salary);
    Integer empid=(Integer)session.save(e1);
    tx.commit();
    session.close();
    return empid;

}
   /* Method to  READ all the employees */

   public void listemployee(){
        Session session = factory.openSession();
        List<employee> emplist = session.createQuery("FROM Employee").list();
        for(employee emp:emplist){
            System.out.print("First name" +emp.getFirstname());
            System.out.print("Last name" +emp.getLastname());
            System.out.println("Salary" +emp.getSalary());
        }
        session.close();
    }

          /* Method to UPDATE salary for an employee */

      public void updateemployee(Integer id,Integer salary)
     {
         Session session = factory.openSession();
         Transaction tx = session.beginTransaction();
         employee emp = (employee)session.get(employee.class, id);
         emp.setSalary(salary);
         session.update(emp);
         tx.commit();
         session.close();
      }

      public void deleteemployee(Integer id)
      {
       Session session = factory.openSession();
       Transaction tx = session.beginTransaction();
       employee emp = (employee)session.get(employee.class, id);
       session.delete(emp);
        tx.commit();
      }
    }


错误:

java - 线程“main” org.hibernate.MappingException中的异常:未知实体:org.hibernate.employee-LMLPHP

项目路径:

java - 线程“main” org.hibernate.MappingException中的异常:未知实体:org.hibernate.employee-LMLPHP

最佳答案

<class name="employee">


应具有完整的软件包名称(路径),并且所有类均应以大写字母开头。

所以应该像这样:

package mypackage;

public class Employee
{
    ...
}

<class name="mypackage.Employee" ...>

09-10 02:08