Java 如何在Hibernate中从数据库中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20781286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fetch data from database in Hibernate
提问by user2782773
This is my class to fetch data from database
这是我从数据库中获取数据的类
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();
}
}
This is my Emplouyee.hbm.xml
file :
这是我的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>
when i run this Program then following Exception come please help me how to Fix it i am new in Hibernate and trying learn but am stuck.
当我运行这个程序然后跟随异常来请帮助我如何修复它我是 Hibernate 的新手并尝试学习但被卡住了。
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException:
EMPLOYEE is not mapped [from EMPLOYEE]
while i am able to store data in database i have 2 class one for String data
and second fetching data Problem is coming in fetching data plz help.
虽然我能够将数据存储在数据库中,但我有 2 类第一类String data
和第二类获取数据问题正在获取数据请帮助。
采纳答案by Ben
Let me quote this:
让我引用一下:
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.
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.
As far as I can see you are using the table name.
据我所知,您正在使用表名。
So it should be like this:
所以它应该是这样的:
Query query = session.createQuery("from Employee");
回答by Scorpion
try the class-name
试试类名
Query query = session.createQuery("from Employee");
instead of the table name
而不是表名
Query query = session.createQuery("from EMPLOYEE");
回答by Dmitry Sh
The correct way from 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 code (can find at Google):
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 */
}
回答by jagadeesh
Query query = session.createQuery("from Employee");
Note: from Employee. here Employee is not your table name it's POJO name.
注:来自员工。这里 Employee 不是你的表名,它是 POJO 名。
回答by RajatN
I know that it is very late to answer the question, but it may help someone like me who spent lots off time to fetch data using hql
我知道现在回答这个问题已经很晚了,但它可能会帮助像我这样花了很多时间使用 hql 获取数据的人
So the thing is you just have to write a query
所以问题是你只需要写一个查询
Query query = session.createQuery("from Employee");
it will give you all the data list but to fetch data from this you have to write this line.
它将为您提供所有数据列表,但要从中获取数据,您必须编写这一行。
List<Employee> fetchedData = query.list();
As simple as it looks.
看起来很简单。
回答by karan
Hibernate has its own sql features that is known as hibernate query language. for retriving data from database using hibernate.
Hibernate 有自己的 sql 特性,称为 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);
}
for fetch we need Iterator for that define and import package.
对于 fetch 我们需要 Iterator 来定义和导入包。