【问题标题】:Exception in thread "main" org.hibernate.MappingException: Unknown entity: from Destination线程“主”org.hibernate.MappingException 中的异常:未知实体:来自目标
【发布时间】:2012-02-19 08:21:24
【问题描述】:

我正在学习休眠,但我不知道为什么会弹出这个错误。我尝试搜索,但找不到对我有帮助的解决方案。我想了解为什么会出现此错误。

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());
//      }
    }
}

当我尝试插入目标(注释代码)时,值被插入到数据库中。

配置:

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>

目标.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>

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java hibernate orm maven-2 hibernate-mapping


    【解决方案1】:

    根据here,这样的事情应该可以工作:

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

    【讨论】:

      【解决方案2】:

      使用session.createCriteria(Destination.class);您正在尝试使用HQL - Hibernate Query Language,您需要使用其他类似的api

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

      【讨论】:

      • 请看我上面贴的代码。我正在使用您为 HQL 提到的相同的东西。有什么原因不起作用?
      • @brainydexter - 你的 HQL 很好,但你应该使用“createQuery”方法而不是“createCriteria”
      【解决方案3】:

      您正在混合使用 HQL 和条件查询。你应该这样做

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

      session.createQuery("from Destination").list();
      

      【讨论】:

        【解决方案4】:
        <mapping class="org.wah.dao.Destination"
             resource="org/wah/dao/Destination.hbm.xml"/>
        

        代替这个,请将其更改为

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

        Hibernate 会抛出此错误,因为当它期望某个基于 Annotation 的类时,您指的是基于 XML 的 Hibernate 映射。坚持使用两种不同的规则注解或基于 XML。

        【讨论】:

          【解决方案5】:

          对于尝试使用 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>
          

          【讨论】:

            猜你喜欢
            • 2016-01-16
            • 1970-01-01
            • 2016-06-09
            • 2016-03-01
            • 2017-06-03
            • 2023-03-19
            • 1970-01-01
            • 1970-01-01
            • 2014-09-01
            相关资源
            最近更新 更多