这是我从数据库中获取数据的类
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序然后跟随Exception来请帮助我如何修复它我是Hibernate的新手并尝试学习但是卡住了.
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException:
EMPLOYEE is not mapped [from EMPLOYEE]
Run Code Online (Sandbox Code Playgroud)
虽然我能够在数据库中存储数据我有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");
Run Code Online (Sandbox Code Playgroud)
来自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();
}
Run Code Online (Sandbox Code Playgroud)
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 */
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90117 次 |
| 最近记录: |