0

到目前为止,我一直在使用 EclipseLink,但现在我正在尝试使用 Hibernate 4 的 Java SE 项目。我正在尝试执行 NamedQuery,但出现以下异常:

HibernateLog --> 18:05:03 ERROR org.hibernate.internal.SessionFactoryImpl - HHH000177: Error in named query: SubCategory.findAll
org.hibernate.hql.internal.ast.QuerySyntaxException: Subcategory is not mapped [SELECT s from Subcategory s]

这是我的 DBUtils 课程

public class DBUtils {

    static final Logger logger = Logger.getLogger(DBUtils.class);

    private static final ServiceRegistry serviceRegistry;
    private static final SessionFactory sessionFactory;

    /**
     * 
     */
      //initialize session factory;

      static {
          try {
              Configuration conf = new Configuration()
                // mapped classes; 
                .addAnnotatedClass(Category.class)
                .addAnnotatedClass(SubCategory.class)
                .configure();

                             //.....

              serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
              sessionFactory = conf.buildSessionFactory(serviceRegistry);
          } catch (Throwable ex) {
              logger.error("Initial SessionFactory creation failed");
              System.err.println("." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }


      public static synchronized SessionFactory getSessionFactory() {
          return sessionFactory;
      }
}

这是我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
                <!-- Ommitted... -->

        <!-- JDBC connection pool, use Hibernate internal connection pool -->
        <property name="connection.pool_size">5</property>


        <!-- Defines the SQL dialect used in Hibernate application -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>


        <!-- hibernate sql output -->
        <property name="show_sql">false</property>
        <property name="format_sql">false</property>
        <property name="use_sql_comments">false</property>

        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.connection.useUnicode">true</property>
        <property name="hibernate.connection.characterEncoding">utf8</property>
        <property name="hibernate.connection.CharSet">utf8</property>
    </session-factory>
</hibernate-configuration>

最后我的实体都用@Entity("tableName") 注释

我究竟做错了什么?

4

1 回答 1

1

Hibernate 区分大小写,并使用类名在其 HQL 中映射实体。所以

SELECT s from Subcategory s

将引用一个名为Subcategory. 这是错误的。使用实际的类名

SELECT s from SubCategory s

当你的班级被命名SubCategory时,见于

.addAnnotatedClass(SubCategory.class)
于 2013-10-21T15:26:55.393 回答