Hibernate:没有为查询类找到持久化类

时间:2016-01-23 09:29:19

标签: java hibernate

我有错误。这是我的hibernate.cfg.xml:

    <?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="hibernate.bytecode.use_reflection_optimizer">false</property>
            <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="hibernate.connection.password">huyman123</property>
            <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=testHibernate1</property>
            <property name="hibernate.connection.username">sa</property>
            <property name="hibernate.default_catalog">testHibernate1</property>
            <property name="hibernate.default_schema">dbo</property>
            <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
            <property name="hibernate.search.autoregister_listeners">false</property>
            <property name="hibernate.validator.apply_to_ddl">false</property>
             <property name="connection.pool_size">1</property>
             <property name="hibernate.current_session_context_class">thread</property>
              <property name="show_sql">true</property>

            <mapping resource="pojo/Department.hbm.xml" />
            <mapping resource="pojo/Employee.hbm.xml" />
            <mapping resource="pojo/Timekeeper.hbm.xml" />
            <mapping resource="pojo/SalaryGrade.hbm.xml" />
        </session-factory>
    </hibernate-configuration>

这是HibernateUtils:

    private static final SessionFactory sessionFactory = buildSessionFactory();

         private static SessionFactory buildSessionFactory() {
             Configuration configuration = new Configuration();
             configuration.configure();
             ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                     .applySettings(configuration.getProperties()).build();

             // Tạo đối tượng SessionFactory
             SessionFactory sessionFactory = configuration
                     .buildSessionFactory(serviceRegistry);
             return sessionFactory;
         }

         public static SessionFactory getSessionFactory() {
             return sessionFactory;
         }

         public static void shutdown() {
             // Close caches and connection pools
             // Giải phóng cache và Connection Pools.
             getSessionFactory().close();
         }

这是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 Jan 23, 2016 11:32:57 AM by Hibernate Tools 3.4.0.CR1 -->
>         <hibernate-mapping>
>             <class name="Employee" table="EMPLOYEE">
>                 <id name="empId" type="big_decimal">
>                     <column name="EMP_ID" scale="0" />
>                     <generator class="foreign">
>                         <param name="property">employeeByEmpId</param>
>                     </generator>
>                 </id>
>                 <many-to-one name="employeeByMngId" class="pojo.Employee" fetch="select">
>                     <column name="MNG_ID" scale="0" />
>                 </many-to-one>
>                 <one-to-one name="employeeByEmpId" class="pojo.Employee" constrained="true"></one-to-one>
>                 <many-to-one name="department" class="pojo.Department" fetch="select">
>                     <column name="DEPT_ID" not-null="true" />
>                 </many-to-one>
>                 <property name="empName" type="string">
>                     <column name="EMP_NAME" length="50" not-null="true" />
>                 </property>
>                 <property name="empNo" type="string">
>                     <column name="EMP_NO" length="20" not-null="true" unique="true" />
>                 </property>
>                 <property name="hireDate" type="timestamp">
>                     <column name="HIRE_DATE" length="23" not-null="true" />
>                 </property>
>                 <property name="image" type="binary">
>                     <column name="IMAGE" />
>                 </property>
>                 <property name="job" type="string">
>                     <column name="JOB" length="30" not-null="true" />
>                 </property>
>                 <property name="salary" type="double">
>                     <column name="SALARY" precision="53" scale="0" not-null="true" />
>                 </property>
>                 <set name="employeesForMngId" table="EMPLOYEE" inverse="true" lazy="true" fetch="select">
>                     <key>
>                         <column name="MNG_ID" scale="0" />
>                     </key>
>                     <one-to-many class="pojo.Employee" />
>                 </set>
>                 <one-to-one name="employeeByEmpId" class="pojo.Employee"></one-to-one>
>                 <set name="timekeepers" table="TIMEKEEPER" inverse="true" lazy="true" fetch="select">
>                     <key>
>                         <column name="EMP_ID" scale="0" not-null="true" />
>                     </key>
>                     <one-to-many class="pojo.Timekeeper" />
>                 </set>
>             </class>
>         </hibernate-mapping>

错误消息的堆栈跟踪:

  

警告:HHH000183:找不到查询类的持久化类:选择   e来自pojo.Employee e e.empName,e.empNo

的订单

如果在MainProgram中,我将SQL查询更改为:

  

String sql =&#34;从&#34;中选择e; + Employee.class.getSimpleName()+&#34; Ë   按e.empName排序,e.empNo&#34 ;;            错误消息的堆栈跟踪               引起:org.hibernate.hql.internal.ast.QuerySyntaxException:Employee不是   映射

1 个答案:

答案 0 :(得分:0)

String hql = " FROM " + pojo.Employee.class.getSimpleName() + " e ORDER BY e.empName, e.empNo ASC"; 
Query query = session.createQuery(hql);
List<Object> listEmps = query.list();