如何在Hibernate中从数据库中获取数据 [英] how to fetch data from database in Hibernate

查看:118
本文介绍了如何在Hibernate中从数据库中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从数据库中获取数据的类

  package com.javatpoint.mypackage; 

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
导入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); //填充
//配置文件的数据

//创建工厂对象
SessionFactory factory = cfg.buildSessionFactory();

//创建会话对象
会话会话= factory.openSession();

//创建交易对象
Transaction t = session.beginTransaction();

Query query = session.createQuery(from EMPLOYEE);
java.util.List list = query.list();
System.out.println(list);
t.commit();
session.close();




$ b $ p $这是我的 Emplouyee。 hbm.xml file:

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ;
<! - 由Hibernate Tools生成2013年12月5日12:09:18 3.4.0.CR1 - >
< hibernate-mapping default-lazy =false>
< class name =com.javatpoint.mypackage.Employeetable =EMPLOYEE>
< id name =idtype =int>
< column name =ID/>
< generator class =assigned/>
< / id>
< property generated =neverlazy =falsename =firstName
type =java.lang.String>
< column name =FIRSTNAME/>
< / property>
< property generated =neverlazy =falsename =lastName
type =java.lang.String>
< column name =LASTNAME/>
< / property>
< / class>
< / hibernate-mapping>

当我运行这个程序时,然后下面的异常来请帮助我如何修复它我是新的在休眠并尝试学习,但卡住了。

 线程main中的异常org.hibernate.hql.ast.QuerySyntaxException:
EMPLOYEE未映射[来自员工]

虽然我可以将数据存储在数据库中,但我有2第一类为字符串数据和第二次获取数据问题在于获取数据plz帮助。

解决方案< Hibernate创建了一种名为Hibernate Query Language(HQL)的新语言,其语法非常相似到数据库SQL语言。 HQL的主要区别在于HQL使用类名而不是表名,而属性名而不是列名。



据我所见您使用的是表名。



所以它应该是这样的:

  Query query = session.createQuery(from Employee); 


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 :

<?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.

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.

解决方案

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.

As far as I can see you are using the table name.

So it should be like this:

Query query = session.createQuery("from Employee");

这篇关于如何在Hibernate中从数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆