休眠抛出奇怪的错误:类未映射

2022-09-01 13:51:00

这是错误

org.hibernate.hql.ast.QuerySyntaxException: Payment is not mapped [select p from Payment p]

我不明白为什么会抛出这个错误,类应该被映射,因为我将简要地向您展示。我有一个非常基本的配置,比如这个:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html

我尝试将映射定义添加到休眠状态.cfg.xml并且我还尝试以编程方式添加它。他们都没有工作。谁能告诉我我在这里错过了什么?(这不是我第一次把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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/paymentsdatabase</property>
    <property name="hibernate.connection.username">xxx</property>
    <property name="hibernate.connection.password">xxx</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <property name="hibernate.hbm2ddl.auto">create</property>

    <!--  <mapping class="com.lsyh.swati.zk.model.Payment"/> -->
  </session-factory>
</hibernate-configuration>

数据库连接工作正常,我已经测试过了

这是我的HibernateUtil中的静态初始值设定项

static {
    try {

        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration()
            .addPackage("com.lsyh.swati.zk.model")
            .addAnnotatedClass(Payment.class)
            .configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed. " + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

这就是我在PaymentIODb类中使用会话网站的地方:

public static List<Payment> readDataFromDb(){
        StatelessSession session = StoreHibernateUtil.getSessionFactory().openStatelessSession();
        Query query = session.createQuery("select p from Payment p");
        List<Payment> payments = query.list();
        session.close();
        return payments;
}    

这是堆栈跟踪

org.hibernate.hql.ast.QuerySyntaxException: Payment is not mapped [select p from Payment p]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at com.lsyh.swati.zk.controller.PaymentIODb.readDataFromDb(PaymentIODb.java:35)
    at com.lsyh.swati.zk.controller.PaymentIODb.resolveVariable(PaymentIODb.java:20

答案 1

我希望有两件事之一是原因:

  1. 要么你没有在你的休眠中列出付款.cfg.xml要么你在哪里配置你的映射类。

  2. 另一个原因可能是javax之间的混淆...Entity and org.hibernate....实体。确保使用第一个。


答案 2

而不是

Query query = session.createQuery("select p from Payment p");

试试这个

Query query = session.createQuery("select p from " + Payment.class.getName() + " p");

推荐