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

发布于 2024-12-28 22:47:22 字数 3256 浏览 1 评论 0原文

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

线程“main”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());
//      }
    }
}

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

配置:

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>

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

I am learning hibernate and I can't figure out why is this error popping up. I tried searching but I couldn't find a solution that helped me. I'd like to learn why am I getting this error.

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

Here are some details:

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

When I try to insert the destination (commented code), values are being inserted into db.

Configurations:

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>

Can someone please help me figure this out ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随心而道 2025-01-04 22:47:22

use session.createCriteria(Destination.class); 您正在尝试使用HQL - Hibernate 查询语言,您需要使用其他 api,例如

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

use session.createCriteria(Destination.class); You are trying to use HQL - Hibernate Query Language for which you need to use other api like

Query query = session.createQuery("from Destination");
List list = query.list();
婴鹅 2025-01-04 22:47:22
<mapping class="org.wah.dao.Destination"
     resource="org/wah/dao/Destination.hbm.xml"/>

而不是这个,请将其更改为

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

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

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

instead of this, please change it to

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

Hibernate throws this error because when it expect some Annotation based class, you are referring the XML based Hibernate mapping. Stick to two different rule Annotations or XML based.

守护在此方 2025-01-04 22:47:22

您正在混合 HQL 和 Criteria 查询。你应该做

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

或者

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

You are mixing HQL and Criteria query. You should either do

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

or

session.createQuery("from Destination").list();
救赎№ 2025-01-04 22:47:22

根据这里,这样的东西应该可以工作:

列表结果 = session.createCriteria(Destination.class).list();

According to here, something like this should work:

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

檐上三寸雪 2025-01-04 22:47:22

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

I had similar problem for a simple Console application trying to use Hibernate. The solution I arrived to make the add the "packagesToScan" property explicitly for LocalSessionFactoryBean.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文