2012-01-26 11 views
1

私は休止状態を学習しており、なぜこのエラーがポップアップしているのかわかりません。私は検索を試みたが、私を助けた解決策を見つけることができなかった。なぜこのエラーが出るのかを知りたい。ここスレッド "main"の例外org.hibernate.MappingException:不明なエンティティ:宛先から

Exception in thread "main" org.hibernate.MappingException: Unknown entity: from Destination

いくつかの詳細は次のとおり

main(): 

    public static void main(String[] args) { 
     Session session = HibernateUtil.getSessionFactory().openSession(); 
     session.beginTransaction(); 

//  Destination destination = new Destination(); 
//  destination.setName("IDelhi"); 
//  destination.setLatitude(1.0f); 
//  destination.setLongitude(123.0f); 
//  session.save(destination); 

     List result = session.createCriteria("from Destination").list(); 

     session.getTransaction().commit(); 

     session.close(); 

//  for (Object dest : result) { 
//   Destination d = (Destination)dest; 
//   System.out.println(d.getId() + ": "+ d.getName()); 
//  } 
    } 
} 

I宛先(コメントコード)を挿入しようとすると、値は、DBに挿入されています。

構成:

は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 name=""> 
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 
    <property name="hibernate.connection.password">*****</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/</property> 
    <property name="hibernate.connection.username">*****</property> 
    <property name="hibernate.default_schema">wah_schema</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 
    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 
    <mapping class="org.wah.dao.Destination" resource="org/wah/dao/Destination.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

Destination.hbm.xml:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Jan 25, 2012 3:31:00 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
<class name="org.wah.dao.Destination" table="DESTINATION"> 
    <id name="id" type="int"> 
    <column name="ID"/> 
    <generator class="identity"/> 
    </id> 
    <property generated="never" lazy="false" name="name" type="java.lang.String"> 
    <column name="NAME"/> 
    </property> 
    <property generated="never" lazy="false" name="latitude" type="float"> 
    <column name="LATITUDE"/> 
    </property> 
    <property generated="never" lazy="false" name="longitude" type="float"> 
    <column name="LONGITUDE"/> 
    </property> 
</class> 
</hibernate-mapping> 

は、誰かが私はこれを理解助けてくださいことはできますか?

答えて

3

使用session.createCriteria(Destination.class);あなたは他のAPIを使用する必要があるためHQL - Hibernate Query Languageよう

Query query = session.createQuery("from Destination"); 
List list = query.list(); 
+0

Plsは上に掲載したコードを参照してください。私はあなたがHQLについて言及したものと同じことを使用しています。それがうまくいかない理由は何ですか? – brainydexter

+1

@brainydexter - あなたのHQLは良いですが、 "createCriteria"ではなく "createQuery"メソッドを使うべきです –

+0

それを指摘してくれてありがとう!私はそれを見落とした。 – brainydexter

0

hereによると、このような何かが動作するはずです:

List result = session.createCriteria(Destination.class).list();

1

あなたを使用しようとしていますHQLとCriteriaクエリが混在しています。あなたは、

session.createCriteria(Destination.class).list(); 

または

session.createQuery("from Destination").list(); 
2
<mapping class="org.wah.dao.Destination" 
    resource="org/wah/dao/Destination.hbm.xml"/> 

それはいくつかのアノテーションベースのクラスを期待していたときので、これに代えて、

<mapping resource="org/wah/dao/Destination.hbm.xml"> 

にそれを変更してくださいHibernateはこのエラーをスローを行う必要がありますどちらかXMLベースのHibernateマッピングを参照しています。 2つの異なるルールアノテーションまたはXMLベースに固執する。

0

Hibernateを使用しようとしている単純なコンソールアプリケーションでも同様の問題がありました。私がLocalSessionFactoryBeanに対して明示的に "packagesToScan"プロパティを追加するようになったのは解決策でした。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="com.mg.learning.spring.orm"/> <--- this SOLVED! 
    <property name="hibernateProperties"> 
    <props> 
     <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
    </props> 
    </property> 
</bean> 
関連する問題