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

时间:2016-10-21 19:11:57

标签: java hibernate

不确定是什么问题。我得到了例外情况:

  

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>

client.java

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

错误:

enter image description here

项目路径:

enter image description here

1 个答案:

答案 0 :(得分:0)

<class name="employee">

应该有完整的包名(路径),所有类都应以大写字母开头。

所以应该是这样的:

package mypackage;

public class Employee
{
    ...
}

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