Hibernate抛出奇怪的错误:类未映射 [英] Hibernate throws strange error: Class is not mapped

查看:77
本文介绍了Hibernate抛出奇怪的错误:类未映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误

  org.hibernate.hql.ast.QuerySyntaxException:付款未被映射[从付款中选择p p] 

我不明白如何引发这个错误,该类应该被映射为I会简要地向你展示。我有一个非常基本的配置,像这样: http: //docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html



我试图添加映射定义到hibernate.cfg.xml中,我也尝试以编程方式添加它。他们都没有工作。有谁能告诉我我在这里错过了什么? (这不是我第一次把Hibernate项目放在一起)



这是hibernate.cfg.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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>线程< / property>

< property name =hibernate.hbm2ddl.auto>建立< / property>

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

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

这是我的HibernateUtil中的静态初始化器

  static {
try {

//从标准(hibernate.cfg.xml)
// config文件创建SessionFactory。
sessionFactory = new AnnotationConfiguration()
.addPackage(com.lsyh.swati.zk.model)
.addAnnotatedClass(Payment.class)
.configure()。buildSessionFactory ();
} catch(Throwable ex){
//记录异常。
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);






这就是我在PaymentIODb中使用sessionFactory的地方class:

  public static List< Payment> readDataFromDb(){
StatelessSession session = StoreHibernateUtil.getSessionFactory()。openStatelessSession();
Query query = session.createQuery(从付款p中选择p);
列表<付款> payments = query.list();
session.close();
退货支付;
}

这是堆栈跟踪

  org.hibernate.hql.ast.QuerySyntaxException:付款未映射[从付款p选择p] 
在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 )
在org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
在org.hibernate.hql.antlr.HqlSqlBaseWalker.selec tStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl。
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) org.hibernate.engine.query.HQLQueryPlan。< init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan。< init>(HQLQueryPlan.java:
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.Paymen tIODb.resolveVariable(PaymentIODb.java:20


解决方案

d期待以下两个原因之一:


  1. 或者您没有在您的hibernat.cfg中列出付款。 xml或任何你配置映射类的地方。

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



this is the error

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

I don't understand how come this error is thrown, the class should be mapped as I will show you briefly. I have a very basic config, like this one: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html

I have tried to add the mapping definition into hibernate.cfg.xml and I have also tried to add it programmatically. Neither of them worked. Could anybody tell me what am I missing here? (it is not the first time I put together a Hibernate project)

this is the 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>

the database connection is working fine, I have tested that

this is the static initializer in my 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);
    }
}

and this is where I use the sessionFactory in the PaymentIODb class:

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

this is the stack trace

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

解决方案

I'd expect one of two things to be the reason:

  1. either you don't have Payment listed in your hibernat.cfg.xml or where ever you config your mapped classes.

  2. another reason might be the confusion between javax...Entity and org.hibernate....Entity. Make sure you use the first one.

这篇关于Hibernate抛出奇怪的错误:类未映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆