I have written a simple java class to fetch data using HQL. This is a standalone application and my database is not local. I have made the necessary entries in the hibernate.cfg.xml. However I am not sure of the entries and get the following error when I try to run the application -
[java] net.sf.hibernate.QueryException: in expected: b [select b from TransactionLog b where b.transSrcSystem = 'RFI']
[java] at net.sf.hibernate.hql.FromParser.token(FromParser.java:102)
[java] at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
[java] at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
[java] at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
[java] at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
[java] at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
[java] at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:295)
[java] at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1572)
[java] at net.sf.hibernate.impl.SessionImpl.iterate(SessionImpl.java:1602)
[java] at net.sf.hibernate.impl.QueryImpl.iterate(QueryImpl.java:29)
[java] at com.echostar.requestforinfo.hibernate.createSession.main(Unknown Source)
I have searched various websites for this error and most of them suggest to check the mappings in the hibernate.cfg.xml file. Can anyone please go through the attached code and help? Any help will be highly appreciated.
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin@dcorpdb:1522:DCORP</property>
<property name="connection.username">wls_bp</property>
<property name="connection.password">wls_bp</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<mapping resource="com/echostar/requestforinfo/hibernate/TransactionLog.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.echostar.requestforinfo.hibernate;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.JDBCException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class createSession
{
private Log LOG = LogFactory.getLog( createSession.class.getName() );
public static void main ( String[] args )
{
try
{
System.out.println("App Started");
Configuration cfg = new Configuration();
SessionFactory sessions = cfg.buildSessionFactory();
System.out.println("AFter creating sessions");
Transaction tx = null;
Session session = sessions.openSession();
Query query = session.createQuery(
"select b from TransactionLog b where b.transSrcSystem = 'RFI'");
System.out.println("AFter creating query"+query.getQueryString());
Iterator result = query.iterate();
int count = 0;
while(result.hasNext())
{
count++;
}
System.out.println("Number of Objects: " + count);
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.echostar.requestforinfo.hibernate.TransactionLog" table="BP_TRANSACTION_LOG">
<id name="transactionid" column="TRANSACTION_LOG_ID" type="long">
<generator class="sequence">
<param name="sequence">BP_TRANSACTION_LOG_SEQ</param>
</generator>
</id>
<property name="transLogId" column="TRANSACTION_LOG_ID"/>
<property name="appBodyInfo" column="APP_BODY_INFO"/>
<property name="transDate" column="TRANSACTION_DATE"/>
<property name="transSrcSystem" column="TRANSACTION_SOURCE_SYSTEM"/>
</class>
</hibernate-mapping>
My database details are as follows -
username=wls_bp
password=wls_bp
host=dcorpdb
protocol=tcp
port=1522
sid=DCORP