-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: URGENT:Unable to retrieve records through HIbernate
PostPosted: Sun Aug 21, 2011 3:29 am 
Newbie

Joined: Sun Aug 21, 2011 2:55 am
Posts: 2
Hi
I am a newbie for hibernate. This is the issue with a first example given in the Hibernate documentation.I used Oracle 10g/MySQL/Sql Server 2k5 and I am unable to retrieve records from those database. Here is the following configurations:
emp.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class  name="consoleapphiber.Emp2Bean" table="Emp">
          <id name="id"  column="id" >
              <generator class="increment"/>
          </id>
          <property name="dob" type="timestamp" column="dob"/>
          <property name="age" type="int" column="age"/>
          <property name="fname" type="string" column="fname"/>
            </class>
             
    </hibernate-mapping>

hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://localhost:1477;database=testdb;</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">mogamboo</property>
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="show_sql">true</property>
      <property name = "hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
     <mapping resource="emp.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Emp2Bean.java
Code:
package consoleapphiber;
public class Emp2Bean {
private String fname;
private int age;
private java.util.Date dob;
private long id;
    public Emp2Bean(){}
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public java.util.Date getDob() {
        return dob;
    }
    public void setDob(java.util.Date dob) {
        this.dob = dob;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

<b>EmpManager.java</b>
Code:
package consoleapphiber;
import org.hibernate.Transaction;
import org.hibernate.Session;
import java.util.*;
public class EmpManager {

    public static void main(String[] args) {
        EmpManager mgr = new EmpManager();

       // if (args[0].equals("store")) {
         //   mgr.createAndStoreEmp();
        //}
        List list = mgr.getEmployees();
        HibernateUtil.sessionFactory.close();
        System.out.println("Successfully inserted .,...............");
        System.out.println(list);
       
    }
    private List getEmployees()
    {
    Session session = HibernateUtil.getCurrentSession();
    Transaction tx = session.beginTransaction();
    List list = session.createQuery("from Emp").list();
    tx.commit();
    session.close();
    return list;
    }
private void createAndStoreEmp() {
    Session session = HibernateUtil.getCurrentSession();
    Transaction tx = session.beginTransaction();

    Emp2Bean empbean = new Emp2Bean();
    empbean.setAge(20);
    empbean.setFname("Senthil Kumar");
    empbean.setDob(new Date());

    session.save(empbean);

    tx.commit();
    HibernateUtil.closeSession();
}

}

<b>HibernateUtil.java</b>
Code:
package consoleapphiber;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
    public static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session getCurrentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}


I am getting this exception when I run the code:
Code:
Hibernate: select  from
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.loader.Loader.doList(Loader.java:2150)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
        at org.hibernate.loader.Loader.list(Loader.java:2021)
        at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:866)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:992)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
        at consoleapphiber.EmpManager.getEmployees(EmpManager.java:36)
        at consoleapphiber.EmpManager.main(EmpManager.java:26)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'from'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
        at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:137)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1676)
        at org.hibernate.loader.Loader.doQuery(Loader.java:662)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
        at org.hibernate.loader.Loader.doList(Loader.java:2147)
        ... 7 more
Java Result: 1

Please help me it is very urgent.
Thanks in advance.
Regards
Sentil


Top
 Profile  
 
 Post subject: Re: URGENT:Unable to retrieve records through HIbernate
PostPosted: Sun Aug 28, 2011 3:47 am 
Newbie

Joined: Sun Aug 21, 2011 2:55 am
Posts: 2
Hi


I have found the answer for my problem:
The line :
List list = session.createQuery("from Emp").list();
in EmpManager.java
should be replaced by
List list = session.createQuery("from Emp2Bean").list()
This solves the problem.
Henceforth the syntax is :
List list = session.createQuery("from <<MAPPED-CLASS-NAME>>").list();
Thanks
Senthil


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.