user2782773 user2782773 - 10 months ago 55
Java Question

how to fetch data from database in Hibernate

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();

This is my
file :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- 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" />
<property generated="never" lazy="false" name="firstName"
<column name="FIRSTNAME" />
<property generated="never" lazy="false" name="lastName"
<column name="LASTNAME" />

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.

Ben Ben
Answer Source

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");