这是我从数据库中获取数据的类
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>
当我运行此程序然后跟随Exception时,请帮助我如何修复它我是Hibernate的新手并尝试学习但是卡住了。
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException:
EMPLOYEE is not mapped [from EMPLOYEE]
虽然我能够在数据库中存储数据,但我有{1}的第2级和第二次获取数据问题正在获取数据plz帮助。
答案 0 :(得分:7)
让我引用这个:
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");
答案 1 :(得分:6)
来自hibernate doc的正确方法:
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
// here get object
List<Employee> list = s.createCriteria(Employee.class).list();
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
Logger.getLogger("con").info("Exception: " + ex.getMessage());
ex.printStackTrace(System.err);
} finally {
s.close();
}
HibernateUtil代码(可在Google上找到):
public class HibernateUtil {
private static final SessionFactory tmrSessionFactory;
private static final Ejb3Configuration tmrEjb3Config;
private static final EntityManagerFactory tmrEntityManagerFactory;
static {
try {
tmrSessionFactory = new Configuration().configure("tmr.cfg.xml").buildSessionFactory();
tmrEjb3Config = new Ejb3Configuration().configure("tmr.cfg.xml");
tmrEntityManagerFactory = tmrEjb3Config.buildEntityManagerFactory();
} catch (HibernateException ex) {
Logger.getLogger("app").log(Level.WARN, ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return tmrSessionFactory;
}
/* getters and setters here */
}
答案 2 :(得分:2)
尝试使用类名
Query query = session.createQuery("from Employee");
而不是表名
Query query = session.createQuery("from EMPLOYEE");
答案 3 :(得分:1)
我知道现在回答这个问题已经很晚了,但它可能会帮助像我一样花很多时间使用hql获取数据的人
所以你只需编写一个查询
Query query = session.createQuery("from Employee");
它将为您提供所有数据列表,但是要从中获取数据,您必须编写此行。
List<Employee> fetchedData = query.list();
看起来很简单。
答案 4 :(得分:1)
Hibernate有自己的sql功能,称为hibernate查询语言。 使用hibernate从数据库中恢复数据。
String sql_query = "from employee"//user table name which is in database.
Query query = session.createQuery(sql_query);
//for fetch we need iterator
Iterator it=query.iterator();
while(it.hasNext())
{
s=(employee) it.next();
System.out.println("Id :"+s.getId()+"FirstName"+s.getFirstName+"LastName"+s.getLastName);
}
对于fetch,我们需要Iterator来定义和导入包。
答案 5 :(得分:0)
Query query = session.createQuery("from Employee");
注意:来自员工。这里的员工不是你的表名,而是POJO的名字。