本文介绍了org.hibernate.hql.internal.ast.QuerySyntaxException:表未映射[来自表]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$我试图学习如何使用Hibernate使用MySQL,我从来没有在错误链之前和之后使用过它。 p $ p> 线程main中的异常org.hibernate.hql.internal.ast.QuerySyntaxException:国家未映射[来自国家]
位于org.hibernate.hql.internal.ast .util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
在org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
在org.hibernate.hql .internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate .hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3291)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3180)
在org.hibernate作为.hql.internal.antlr.HqlSqlBaseWalker.fromClause( HqlSqlBaseWalker.java:706)美元,org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562 B $ B)
在org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement( HqlSqlBaseWalker.java:299)美元,org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247 b $ b)
在org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze( QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile( QueryTranslatorImpl.java:136)
在org.hibernate.engine.query.spi.HQLQueryPlan。< init>(HQLQueryPlan.java:105)
在org.hibernate.engine.query.spi.HQLQueryPlan 。< init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl。 getHQLQueryPlan(AbstractSessionI
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735)$ b在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
$ b $ $ b at org.hibernate.tutorial.hbm.Test.main(Test.java:37)

我尝试了解其他问题的可能解决方案,但没有任何结果,它无效。这是我迄今为止所做的:



hibernate.cfg.xml (文件夹src)

 <?xml version ='1.0 'encoding ='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0 .dtd>

< hibernate-configuration>
< session-factory>
< property name =connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =connection.url> jdbc:mysql:// localhost:3306 / stock< / property>
< property name =connection.username> root< / property>
< property name =connection.password> root< / property>

< property name =connection.pool_size> 1< / property>

< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>

< property name =show_sql> true< / property>
< property name =format_sql> true< / property>
< property name =hbm2ddl.auto>建立< / property>

< property name =c3p0.acquire_increment> 1< / property>
< property name =c3p0.idle_test_period> 3600< / property> <! - 秒 - >
< property name =c3p0.min_size> 3< / property>
< property name =c3p0.max_size> 5< / property>
< property name =c3p0.max_statements> 0< / property>
< property name =c3p0.timeout> 3605< / property> <! - 秒 - >
< property name =hibernate.c3p0.preferredTestQuery> select 1;< / property>

<! - < mapping resource =country.hbm.xml/> - >
< / session-factory>
< / hibernate-configuration>

country.hbm.xml (文件夹* src\ main \resources\org\hibernate\tutorial\hbm *

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0 .dtd>
< hibernate-mapping>
< class name =org.hibernate.tutorial.hbm.countrytable =country>
< id name =id_country>
< property name =country/>
< / class>
< / hibernate-mapping>

country.java

  package org.hibernate.tutorial.hbm; 

公共类国家{
private int id_country;
私人字符串国家;

public int getId_country(){
return id_country;
}
public void setId_country(int id_country){
this.id_country = id_country;
}
public String getCountry(){
return country;
}
public void setCountry(String country){
this.country = country;
}

}

Test.java

  package org.hibernate.tutorial.hbm; 

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
// import org.hibernate.Transaction;
导入org.hibernate.cfg.Configuration;
import org.hibernate.tutorial.hbm.country; / *仅用于测试函数* /

public class Test {

private static final SessionFactory sessionFactory;

static {
try {
//使用hibernate.cfg.xml的内容初始化工厂$ b $ sessionFactory = new Configuration()。configure()。buildSessionFactory() ;
} catch(Throwable ex){
System.err.println(Initial SessionFactory creation failed。+ ex);
抛出新的ExceptionInInitializerError(ex);
}

}

public static SessionFactory getSessionFactory(){
return sessionFactory;


public static void main(String [] args){

country mycls = new country();
/ * id字段将自动递增到一个唯一的值,以区分记录* /
//mycls.setCountry(\"My Property);
Session session = sessionFactory.openSession();
session.beginTransaction();
列表结果= session.createQuery(from country).list(); (国家ctry:(List< country>结果)){
System.out.println(Country(+ ctry.getId_country()+):+ ctry.getCountry())
;
}
session.getTransaction()。commit();
session.close(); / *如果此测试成功,您将在您的MySQL数据库中找到新记录* /

}

}



- 编辑20130301



I已经修改了几条建议

hibernate.cfg.xml

 <! - 映射文件 - > 
< mapping resource =org / hibernate / tutorial / hbm / Country.hbm.xml/>

我重命名并移动了文件Country.hbm.xml(这是country.hbm。 xml)并更改了这个

 < class name =Countrytable =country> 
<! - < class name =org.hibernate.tutorial.hbm.countrytable =country> - >

该文件已移至 src\org\hibernate\tutorial\hbm b
$ b

Country.java类重命名为Country.java

最后这行在Test.java中被修改

  List result = session.createQuery (来自国家).list(); (Country ctry:(List< Country>)结果){
System.out.println(Country(+ ctry.getId_country()+):+ ctry.getCountry())
;
}

现在我收到了这个错误

 创建初始SessionFactory failed.org.hibernate.InvalidMappingException:无法从资源org / hibernate / tutorial / hbm / Country.hbm.xml 
线程main中的异常java.lang.ExceptionInInitializerError $ b $在org.hibernate.tutorial.hbm.Test。< clinit>(Test.java:21)
导致:org.hibernate.InvalidMappingException:无法解析来自资源org / hibernate / tutorial / hbm / Country.hbm.xml的映射文档
在org.hibernate.cfg.Configuration $ MetadataSourceQueue.processHbmXml(Configuration.java:
at org.hibernate.cfg.Configuration $ MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3412)
at org.hibernate.cfg.Configuration $ MetadataSourceQueue.processMetadata(Configuration.java:3400)
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330)
在o rg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)
位于org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1781)
位于org.hibernate.tutorial.hbm。 < clinit>(Test.java:18)
引起:org.hibernate.MappingException:class寻找属性时找不到国家:id_country $ b $ org.hibernate.internal.util.ReflectHelper .reflectedPropertyClass(ReflectHelper.java:232)
在org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:324)在org.hibernate.cfg.HbmBinder.bindSimpleId
(HbmBinder.java:449 )在org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues
(HbmBinder.java:382)
在org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:322)
。在组织。 hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
at org.hibernate.cfg.Configuration $ MetadataSourceQueue.processHbmXml(Configuration.java:3420)
... 6 more
造成者:java.lan g.ClassNotFoundException:国家在java.net.URLClassLoader
$ 1.run(未知源)$ b $在java.net.URLClassLoader $ 1.run(未知源)$ b $在java.security.AccessController。在Java.net.URLClassLoader.findClass中使用
(未知源代码)$ java.util.ClassLoader.loadClass中的b $ b(未知源代码)在sun.misc.Launcher $ AppClassLoader中
。 loadClass(Unknown Source)$ java.util.ClassLoader.loadClass中的b $ b(未知源)$ java.util.Class.forName0中的
(本地方法)$ b $ java.util.Class.forName(未知来源)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228)
... 12 more

感谢您的帮助。谢谢。

解决方案

我终于解决了这个问题,编辑了答案中的所有内容, Country.hbm.xml文件为此:(注意注释行)

 <?xml version =1.0编码= UTF-8 >?; 
<!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0 .dtd>
< hibernate-mapping>
<! - < class name =Countrytable =country> - >
< class name =org.hibernate.tutorial.hbm.Countrytable =country>
< id name =id_country/>
< property name =country/>
< / class>
< / hibernate-mapping>

它必须是 org.hibernate.tutorial.hbm.Country 而不是 org.hibernate.tutorial.hbm.country


I'm trying to learn to use Hibernate using MySQL, I've never worked with it before and after a chain of errors I finally get stucked with this:

Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: country is not mapped [from country]
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3291)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3180)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735)
    at org.hibernate.tutorial.hbm.Test.main(Test.java:37)

I've tried with possible solutions of another questions, but nothing, it doesn't work. This is what I've done so far:

hibernate.cfg.xml (Folder src)

<?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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/stock</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <!-- configuration pool via c3p0-->
        <property name="c3p0.acquire_increment">1</property>
        <property name="c3p0.idle_test_period">3600</property> <!-- seconds -->
        <property name="c3p0.min_size">3</property>
        <property name="c3p0.max_size">5</property>
        <property name="c3p0.max_statements">0</property>
        <property name="c3p0.timeout">3605</property> <!-- seconds -->
        <property name="hibernate.c3p0.preferredTestQuery">select 1;</property>

        <!-- Mapping files -->
        <!--<mapping resource="country.hbm.xml"/> -->
    </session-factory>
</hibernate-configuration>

country.hbm.xml (Folder *src\main\resources\org\hibernate\tutorial\hbm*)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="org.hibernate.tutorial.hbm.country" table="country">
        <id name="id_country">
        <property name="country"/>
    </class>
</hibernate-mapping>

country.java

package org.hibernate.tutorial.hbm;

public class country {
    private int id_country;
    private String country;

    public int getId_country() {
        return id_country;
    }
    public void setId_country(int id_country) {
        this.id_country = id_country;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

}

Test.java

package org.hibernate.tutorial.hbm;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
//import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tutorial.hbm.country; /* required for test function only */

public class Test {

    private static final SessionFactory sessionFactory;

        static {
            try {
                // Initialize factory using contents of hibernate.cfg.xml
                sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }

        }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void main (String[] args) {

        country mycls = new country();
        /* the id field will auto-increment to a unique value which will distinguish records */
        //mycls.setCountry("My Property");
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List result = session.createQuery( "from country" ).list();
        for ( country ctry : (List<country>) result ) {
            System.out.println( "Country (" + ctry.getId_country() + ") : " + ctry.getCountry() );
        }
        session.getTransaction().commit();
        session.close(); /* if this test succeeds, you'll find a new record in your MySQL database */

    }

}

- Edit 20130301

I've altered with yout suggestions several things

hibernate.cfg.xml

<!-- Mapping files -->
<mapping resource="org/hibernate/tutorial/hbm/Country.hbm.xml"/>

I renamed and moved the file Country.hbm.xml (It was country.hbm.xml) and changed this

<class name="Country" table="country">
<!-- <class name="org.hibernate.tutorial.hbm.country" table="country"> -->

The file was moved to src\org\hibernate\tutorial\hbm

The class country.java was renamed to Country.java

And finally this line was modified in the Test.java

List result = session.createQuery( "from Country" ).list();
        for ( Country ctry : (List<Country>) result ) {
            System.out.println( "Country (" + ctry.getId_country() + ") : " + ctry.getCountry() );
        }

Now I'm getting this as an error

Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource org/hibernate/tutorial/hbm/Country.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.hibernate.tutorial.hbm.Test.<clinit>(Test.java:21)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource org/hibernate/tutorial/hbm/Country.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3423)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3412)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3400)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1330)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1730)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1781)
    at org.hibernate.tutorial.hbm.Test.<clinit>(Test.java:18)
Caused by: org.hibernate.MappingException: class Country not found while looking for property: id_country
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:232)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:324)
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:449)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:382)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:322)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:173)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3420)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: Country
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228)
    ... 12 more

I appreciate your help. Thank you.

解决方案

I finally solve the problem, after editing all of the things in the answer as I show there, I modified the Country.hbm.xml file to this: (Notice the commented line)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <!-- <class name="Country" table="country"> -->
    <class name="org.hibernate.tutorial.hbm.Country" table="country">
        <id name="id_country"/>
        <property name="country"/>
    </class>
</hibernate-mapping>

It has to be org.hibernate.tutorial.hbm.Country and not org.hibernate.tutorial.hbm.country

这篇关于org.hibernate.hql.internal.ast.QuerySyntaxException:表未映射[来自表]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:21