3

私はMySQLを使用してHibernateを使用することを学ぼうとしています.エラーの連鎖の前後にそれを使ったことはありません.

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)

別の質問の可能な解決策を試しましたが、何も機能しません。これは私がこれまでに行ったことです:

hibernate.cfg.xml (フォルダ 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 (フォルダ * 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>

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

}

テスト.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 */

    }

}

- 20130301 を編集

私はあなたの提案でいくつかのことを変更しました

hibernate.cfg.xml

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

ファイルCountry.hbm.xml(country.hbm.xmlでした)の名前を変更して移動し、これを変更しました

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

ファイルはsrc\org\hibernate\tutorial\hbmに移動されました

クラス country.java の名前が Country.java に変更されました

そして最後に、この行は Test.java で変更されました

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

今、私はこれをエラーとして取得しています

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

私はあなたの助けに感謝します。ありがとうございました。

4

2 に答える 2

27

あなたのcountry.hbm.xml形式が正しくありません:

<id name="id_country">

閉じないオープンタグです。私はあなたが意味したと思います:

<id name="id_country"/>

(最後のスラッシュに注意してください)。

次に(@RameshKが指摘したように)、追加する必要があります

<mapping resource="org/hibernate/tutorial/hbm/country.hbm.xml"/>

あなたのhibernate.cfg.xmlファイルに。

1 つの小さな問題: 通常のコーディング規則に従って、Java のクラスは大文字で始まるためcountryCountry.

于 2013-02-28T20:14:24.060 に答える
3

そこに示すように回答のすべてのものを編集した後、最終的に問題を解決し、Country.hbm.xmlファイルを次のように変更しました:(コメント行に注意してください)

<?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>

である必要がorg.hibernate.tutorial.hbm.Countryあり、そうではないorg.hibernate.tutorial.hbm.country

于 2013-03-01T16:48:57.533 に答える