Sunday, March 4, 2012

Hibernate Error : org.hibernate.hql.ast.QuerySyntaxException: TestTable is not mapped [from TestTable}

Problem : Getting an exception while trying to get all the records from a TestTable.:

Following is the Hibernate Config for TestTable:


<hibernate-mapping>
    <class name="com.testapp.service.dataobjects.TestTableDO" table="testTable">
    <id name="testId" column="testId">
        <generator class="assigned"/>
    </id>
...
...
</class>
</hibernate-mapping>




And Following is the Java code to get all the records from the TestTable

List<TestTableDO> testTableResultSet = hibernateTemplate.find("from TestTable");



Solution: The problem is due to the name mismatch in the Hibernate table configurations and the Java Query.

As you can see the hibernate is configured to map the 'testTable' to the 'com.testapp.service.dataobjects.TestTableDO' whereas in Java it refers to 'TestTable' (database table name).
Because we have mapped it to 'TestTableDO' it should be referred as same in Java Query as follows:



List<TestTableDO> testTableResultSet = hibernateTemplate.find("from TestTableDO");

Cheers :)



Error Logs
org.hibernate.hql.ast.QuerySyntaxException: TestTable is not mapped [from TestTable]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)

Thursday, March 1, 2012

Shell Script to check and remove file with specific extension

help(){
        echo " This script checks for the employee files 'test_id001.dat' and delete the files with '*.dat' extension in the directory ${TEST_DIR}. Note : Make sure the TEST_DIR path is set as environment variable."

        exit;

}
if [[ $1 = "-help" ]]
        then
                help
fi

cd ${TEST_DIR}

for file in *.rules
do

## Extract a substring to get the Employee Id from the File Name
## i.e Employeed Id 'id001' in the file test_id001.dat
employeeid="${file##*_}"
employeeid="${employeddid%.*}"

echo " = Remove the file for Employee with ID - $employeeid, file location = ${TEST_DIR}$file"
rm -f ${TEST_DIR}$file

done