gpt4 book ai didi

java - 线程 "main"中的异常 org.hibernate.MappingException : Unknown entity: from Destination

转载 作者:行者123 更新时间:2023-11-29 07:16:38 24 4
gpt4 key购买 nike

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

线程“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>

目的地.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.createCriteria(Destination.class); 您正在尝试使用 HQL - Hibernate Query Language 您需要使用其他 api,例如

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

关于java - 线程 "main"中的异常 org.hibernate.MappingException : Unknown entity: from Destination,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com