Hibernate throws "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MemorySpace is not mapped"


#1

Working project is being upgraded to use Hibernate 5.1.15.Final from hibernate 3.
Table name is memoryspace & entity Name is MemorySpace.

Getting the below error after hibernate upgrade.

2018-08-31 16:26:18,628 ERROR [oncommand] [taskScheduler-1] [o.s.s.s.TaskUtils$LoggingErrorHandler] Unexpected error occurred in scheduled task.
org.hibernate.hql.internal.ast.QuerySyntaxException: MemorySpace is not mapped [DELETE FROM MemorySpace WHERE dataCollectionTimestamp <= :expirationTime]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
	at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
	at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
	at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.company.project.persist.hibernate.HibernateSessionProxyFactoryBean$HibernateSessionInvocationHandler.invoke(HibernateSessionProxyFactoryBean.java:147)
	at com.sun.proxy.$Proxy229.createQuery(Unknown Source)
	at com.company.project.protect.MemorySpaceHistoryManager$6.doInTransactionWithoutResult(MemorySpaceHistoryManager.java:589)
	at org.springframework.transaction.support.TransactionCallbackWithoutResult.doInTransaction(TransactionCallbackWithoutResult.java:34)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
	at com.company.project.protect.MemorySpaceHistoryManager.purgeExpiredTransfers(MemorySpaceHistoryManager.java:562)
	at com.company.project.protect.MemorySpaceHistoryManager.access$100(MemorySpaceHistoryManager.java:81)
	at com.company.project.protect.MemorySpaceHistoryManager$5.run(MemorySpaceHistoryManager.java:191)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at com.company.project.common.logging.MDCClearingThreadFactory$1.run(MDCClearingThreadFactory.java:28)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MemorySpace is not mapped
	at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
	at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
	at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
	at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3690)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3579)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:718)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.deleteStatement(HqlSqlBaseWalker.java:447)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:275)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
	... 28 common frames omitted
@Entity
@Table(name = "memoryspace")
public class MemorySpace implements DomainObject {

}
Query query = session.createQuery( String.format("DELETE FROM %s WHERE %s <= :expirationTime", ClassUtils.getShortClassName(MemorySpace.class), MemorySpaceTransfer_.dataCollectionTimestamp));
query.setParameter("expirationTime", expirationTime);
int numRecords = query.executeUpdate();

This is working code from hibernate 3.
I have cross checked, I have used Entity name in query and not table name.
I also tried using table name - no change in error.
I am not able to figure out why i am getting this error.

Can it be due to infinispan cache(8.2.8.Final) which i am upgrading along with hibernate ?


#2

First of all, you should never use String concatenation or formatting to build queries dynamically:

Query query = session.createQuery( String.format("DELETE FROM %s WHERE %s &lt;= :expirationTime", ClassUtils.getShortClassName(MemorySpace.class), MemorySpaceTransfer_.dataCollectionTimestamp));
query.setParameter("expirationTime", expirationTime); int numRecords = query.executeUpdate();`

You risk an SQL Injection attack.

The proper way to do it is to use Criteria API which works just fine even for bulk UPDATE or DELETE queries.

Now, back to your problem:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: MemorySpace is not mapped

Maybe you are using some HBM mappings that override the annotations, as otherwise, the entity name would match the simple class name. Or, the annotated entities were not scanned by Hibernate.

Try debugging the Hibernate code and check out the entity name as it was mapped by Hibernate.