简体   繁体   中英

Hibernate ORM without XML

I have used EclipseLink till now, but now I'm trying a Java SE project with Hibernate 4. I'm trying to do perform a NamedQuery but I get the following exception:

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]

This is my DBUtils class

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

And this is my 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>

Finally my entities are all annotated with @Entity("tableName" ) annotation

What am I doing wrong?

Hibernate is case sensitive and uses the class name to map an Entity in its HQL. So

SELECT s from Subcategory s

will refer to an entity class called Subcategory . This is wrong. Use the actual class name

SELECT s from SubCategory s

as your class is named SubCategory , seen in

.addAnnotatedClass(SubCategory.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM