我正在学习hibernate,我无法弄清楚为什么会出现这个错误。我试过搜索,但我找不到帮助我的解决方案。我想知道为什么会出现这个错误。
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());
// }
}
}
当我尝试插入目标(注释代码)时,值将被插入到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>
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
使用session.createCriteria(Destination.class);
您正在尝试使用 HQL - Hibernate Query Language ,您需要使用其他api
Query query = session.createQuery("from Destination");
List list = query.list();
答案 1 :(得分:2)
<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。
答案 2 :(得分:1)
您正在混合使用HQL和Criteria查询。你应该做
session.createCriteria(Destination.class).list();
或
session.createQuery("from Destination").list();
答案 3 :(得分:0)
根据here,这样的事情应该有效:
List result = session.createCriteria(Destination.class).list();
答案 4 :(得分:0)
对于试图使用Hibernate的简单控制台应用程序,我遇到了类似的问题。我到达的解决方案是添加&#34; packagesToScan&#34;显式为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>