2015-12-16 205 views
0

我是新的休眠,所以我不知道這個錯誤。 java程序只是試圖顯示,添加和刪除MySQL數據庫中的一些記錄。 我已經閱讀了類似問題的答案,但沒有一個解決了我的問題。 的休眠版本5.0.5org.hibernate.hql.internal.ast.QuerySyntaxException:僱員沒有映射

這裏是主程序我的源代碼:

package ExampleHibernate.ExHiber; 
import java.util.List; import java.util.Date; 
import java.util.Iterator; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

public class ManageEmployee { 
    private static SessionFactory factory; 
    public static void main(String[] args) { 
     try{ 
      Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); 
      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
      factory = configuration.buildSessionFactory(serviceRegistry); 
     }catch (Throwable ex) { 
     System.err.println("Failed to create sessionFactory object." + ex); 
     throw new ExceptionInInitializerError(ex); 
     } 
     ManageEmployee ME = new ManageEmployee(); 

     /* Add few employee records in database */ 
     Integer empID1 = ME.addEmployee("Zara", "Ali", 1000); 
     Integer empID2 = ME.addEmployee("Daisy", "Das", 5000); 
     Integer empID3 = ME.addEmployee("John", "Paul", 10000); 

     /* List down all the employees */ 
     ME.listEmployees(); 

     /* Update employee's records */ 
     ME.updateEmployee(empID1, 5000); 

     /* Delete an employee from the database */ 
     ME.deleteEmployee(empID2); 

     /* List down new list of the employees */ 
     ME.listEmployees(); 
    } 
    /* Method to CREATE an employee in the database */ 
    public Integer addEmployee(String fname, String lname, int salary){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     Integer employeeID = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = new Employee(fname, lname, salary); 
     employeeID = (Integer) session.save(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
     return employeeID; 
    } 
    /* Method to READ all the employees */ 
    public void listEmployees(){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     List employees = session.createQuery("FROM Employee").list(); 
     for (Iterator iterator = employees.iterator(); iterator.hasNext();){ 
      Employee employee = (Employee) iterator.next(); 
      System.out.print("First Name: " + employee.getFirstName()); 
      System.out.print(" Last Name: " + employee.getLastName()); 
      System.out.println(" Salary: " + employee.getSalary()); 
     } 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
    /* Method to UPDATE salary for an employee */ 
    public void updateEmployee(Integer EmployeeID, int salary){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = (Employee)session.get(Employee.class, EmployeeID); 
     employee.setSalary(salary); 
     session.update(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
    /* Method to DELETE an employee from the records */ 
    public void deleteEmployee(Integer EmployeeID){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = (Employee)session.get(Employee.class, EmployeeID); 
     session.delete(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
} 

的POJO:

package ExampleHibernate.ExHiber; 
import javax.persistence.Entity; 

@Entity 
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; 
     } 
    } 

的hibernate.cfg.xml

<?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="hibernate.dialect"> 
     org.hibernate.dialect.MySQLDialect 
    </property> 
    <property name="hibernate.connection.driver_class"> 
     com.mysql.jdbc.Driver 
    </property> 

    <!-- Assume test is the database name --> 
    <property name="hibernate.connection.url"> 
     jdbc:mysql://localhost/testdb 
    </property> 
    <property name="hibernate.connection.username"> 
     test 
    </property> 
    <property name="hibernate.connection.password">admin</property> 

    <!-- List of XML mapping files --> 
    <mapping resource="Employee.hbm.xml"/> 
    <mapping class ="ExampleHibernate.ExHiber.Employee" /> 
</session-factory> 
</hibernate-configuration> 

映射文件:

<?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> 

堆棧跟蹤:

org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [FROM Employee] 
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79) 
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) 
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) 
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) 
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) 
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) 
    at ExampleHibernate.ExHiber.ManageEmployee.listEmployees(ManageEmployee.java:68) 
    at ExampleHibernate.ExHiber.ManageEmployee.main(ManageEmployee.java:33) 
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped 
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171) 
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) 
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:76) 
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) 
    ... 9 more 
org.hibernate.UnknownEntityTypeException: Unable to locate persister: ExampleHibernate.ExHiber.Employee 
    at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792) 
    at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2710) 
    at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164) 
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2648) 
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2635) 
    at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1102) 
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975) 
    at ExampleHibernate.ExHiber.ManageEmployee.updateEmployee(ManageEmployee.java:89) 
    at ExampleHibernate.ExHiber.ManageEmployee.main(ManageEmployee.java:36) 
org.hibernate.UnknownEntityTypeException: Unable to locate persister: ExampleHibernate.ExHiber.Employee 
    at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792) 
    at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2710) 
    at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164) 
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2648) 
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2635) 
    at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1102) 
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975) 
    at ExampleHibernate.ExHiber.ManageEmployee.deleteEmployee(ManageEmployee.java:106) 
    at ExampleHibernate.ExHiber.ManageEmployee.main(ManageEmployee.java:39) 
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [FROM Employee] 
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79) 
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) 
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) 
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) 
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) 
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) 
    at ExampleHibernate.ExHiber.ManageEmployee.listEmployees(ManageEmployee.java:68) 
    at ExampleHibernate.ExHiber.ManageEmployee.main(ManageEmployee.java:42) 
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped 
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171) 
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) 
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:76) 
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:321) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) 
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) 
    ... 9 more 

回答

0

你需要在你的映射文件中使用完全合格的類名:

<hibernate-mapping> 
    <class name="ExampleHibernate.ExHiber.Employee" table="Employee">