如何在Hibernate中从数据库中获取数据
这是我的类,用于从数据库中获取数据
package com.javatpoint.mypackage;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;
public class retrive {
public static void main(String args[]) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file
// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
// creating session object
Session session = factory.openSession();
// creating transaction object
Transaction t = session.beginTransaction();
Query query = session.createQuery("from EMPLOYEE");
java.util.List list = query.list();
System.out.println(list);
t.commit();
session.close();
}
}
这是我的Emplouyee.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 5 Dec, 2013 12:09:18 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping default-lazy="false">
<class name="com.javatpoint.mypackage.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property generated="never" lazy="false" name="firstName"
type="java.lang.String">
<column name="FIRSTNAME" />
</property>
<property generated="never" lazy="false" name="lastName"
type="java.lang.String">
<column name="LASTNAME" />
</property>
</class>
</hibernate-mapping>
当我运行该程序时,出现异常后,请帮助我如何解决它。我是Hibernate的新手,尝试学习但被卡住了。
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: EMPLOYEE is not mapped [from EMPLOYEE]
虽然我能够将数据存储在数据库中,但我有2个用于第一String data
和第二类的数据获取数据,但在获取数据时遇到了问题PLZ帮助。
回答:
让我引述一下:
Hibernate created a new language named Hibernate Query Language (HQL), the
syntax is quite similar to database SQL language. The main difference between
is HQL uses class name instead of table name, and property names instead of
column name.
据我所知,您正在使用表名。
所以应该是这样的:
Query query = session.createQuery("from Employee");
以上是 如何在Hibernate中从数据库中获取数据 的全部内容, 来源链接: utcz.com/qa/415746.html