Java 线程“main”org.hibernate.MappingException中的异常:未知实体:来自目标

Java 线程“main”org.hibernate.MappingException中的异常:未知实体:来自目标,java,hibernate,orm,maven-2,hibernate-mapping,Java,Hibernate,Orm,Maven 2,Hibernate Mapping,我正在学习hibernate,我不明白为什么会出现这个错误。我试着搜索,但找不到一个对我有帮助的解决方案。我想知道为什么我会犯这个错误 线程主org.hibernate.MappingException中的异常:未知实体:来自目标 以下是一些细节: main(): public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession

我正在学习hibernate,我不明白为什么会出现这个错误。我试着搜索,但找不到一个对我有帮助的解决方案。我想知道为什么我会犯这个错误

线程主org.hibernate.MappingException中的异常:未知实体:来自目标

以下是一些细节:

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());
//      }
    }
}
当我尝试插入目标注释代码时,值被插入到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>
有人能帮我解决这个问题吗?

根据,类似的方法应该可以:

列表结果=session.createCriteriaDestination.class.List

根据我们的经验,类似这样的方法应该有效:

列表结果=session.createCriteriaDestination.class.List

使用session.createCriteriaDestination.class;您正在尝试使用需要使用其他api(如

Query query = session.createQuery("from Destination");
List list = query.list();
使用session.createCriteriaDestination.class;您正在尝试使用需要使用其他api(如

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

您正在混合HQL和条件查询。你要么

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


您正在混合HQL和条件查询。你要么

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

请将其改为

<mapping resource="org/wah/dao/Destination.hbm.xml">
Hibernate抛出此错误,因为当它需要一些基于注释的类时,您引用的是基于XML的Hibernate映射。坚持使用两种不同的规则注释或基于XML的注释

请将其改为

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

Hibernate抛出此错误,因为当它需要一些基于注释的类时,您引用的是基于XML的Hibernate映射。坚持使用两种不同的规则注释或基于XML的注释。

对于尝试使用Hibernate的简单控制台应用程序,我遇到了类似的问题。我找到的解决方案是显式地为LocalSessionFactoryBean添加PackageScon属性

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

对于尝试使用Hibernate的简单控制台应用程序,我遇到了类似的问题。我找到的解决方案是显式地为LocalSessionFactoryBean添加PackageScon属性

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

请看我上面贴的代码。我正在使用与您提到的HQL相同的东西。有什么原因不起作用吗?@brainydexter-你的HQL很好,但是你应该使用createQuery方法,而不是createCriteria。请看我上面发布的代码。我正在使用与您提到的HQL相同的东西。有什么原因不起作用吗?@brainydexter-您的HQL很好,但您应该使用createQuery方法,而不是createCriteria