org.hibernate.hql.internal.ast.QuerySyntaxException:96, table is not mapped

I have created new table in openmrs database and ClaimCodeGeneration.java.

package org.bahmni.module.bahmnicore.model;

import org.openmrs.Auditable;
import org.openmrs.BaseOpenmrsObject;

public class ClaimCodeGeneration BaseOpenmrsObject implements Auditable, Serializable{

	private Integer id;

	private String min_value;

	private String max_value;

	private String current_value;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMin_value() {
		return min_value;
	}

	public void setMin_value(String min_value) {
		this.min_value = min_value;
	}

	public String getMax_value() {
		return max_value;
	}

	public void setMax_value(String max_value) {
		this.max_value = max_value;
	}

	public String getCurrent_value() {
		return current_value;
	}

	public void setCurrent_value(String current_value) {
		this.current_value = current_value;
	}
	
	
}

ClaimCodeGenerationDaoImpl

package org.bahmni.module.bahmnicore.dao.impl;

import org.bahmni.module.bahmnicore.dao.ClaimCodeGenerationDao;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;
import org.bahmni.module.bahmnicore.model.ClaimCodeGeneration;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ClaimCodeGenerationDaoImpl implements ClaimCodeGenerationDao {

    private static final Logger log = Logger.getLogger(ClaimCodeGenerationDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public String getAutoGenerateClaimCode() {
        String queryString = "select if(current_value is null,max(min_value)+1,max(current_value)+1) from claim_code_generation";
        Query queryToGetClaimCode = sessionFactory.getCurrentSession().createQuery(queryString);
        return (String) queryToGetClaimCode.uniqueResult();
    }

    @Override
    public String getMaxClaimCode() {
        String queryString = "select max_value from claim_code_generation";
        Query queryToGetMaxClaimCode = sessionFactory.getCurrentSession().createQuery(queryString);
        return (String) queryToGetMaxClaimCode.uniqueResult();
    }

    @Override
    @Transactional
    public int updateCurrentClaimCode(String claimCode) {
        String query = "update claim_code_generation set current_value = '"+claimCode+"' where id=1";
        SQLQuery queryToUpdateCurrentClaimCOde = sessionFactory.getCurrentSession().createSQLQuery(query);
        return queryToUpdateCurrentClaimCOde.executeUpdate();
    }

    @Override
    public ClaimCodeGeneration getAll(String id) {
        String queryString = "select v from claim_code_generation v where v.id=:id";
        Query queryToGetVisitInfo = sessionFactory.getCurrentSession().createQuery(queryString);
        queryToGetVisitInfo.setString("id", id);
        return (ClaimCodeGeneration) queryToGetVisitInfo.uniqueResult();
    }

}

BahmniClaimCodeGenerationServiceImpl

package org.bahmni.module.bahmnicore.service.impl;

import org.bahmni.module.bahmnicore.dao.ClaimCodeGenerationDao;
import org.bahmni.module.bahmnicore.service.BahmniClaimCodeGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.bahmni.module.bahmnicore.model.ClaimCodeGeneration;
import java.util.List;

@Service
public class BahmniClaimCodeGenerationServiceImpl implements BahmniClaimCodeGenerationService {

    private ClaimCodeGenerationDao claimCodeGenerationDao;

    @Autowired
    public BahmniClaimCodeGenerationServiceImpl(ClaimCodeGenerationDao claimCodeGenerationDao) {
        this.claimCodeGenerationDao = claimCodeGenerationDao;
    }

    @Override
    public String getAutoGenerateClaimCode() {
        return claimCodeGenerationDao.getAutoGenerateClaimCode();
    }

    @Override
    public String getMaxClaimCode() {
        return claimCodeGenerationDao.getMaxClaimCode();
    }

    @Override
    public int updateCurrentClaimCode(String claimCode) {
        return claimCodeGenerationDao.updateCurrentClaimCode(claimCode);
    }

    @Override
    public ClaimCodeGeneration getAll(String id) {
        return claimCodeGenerationDao.getAll(id);
    }
}

ClaimCodeGenerationController

package org.bahmni.module.bahmnicore.web.v1_0.controller;

import org.openmrs.Visit;
import org.bahmni.module.bahmnicore.service.BahmniClaimCodeGenerationService;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.bahmni.module.bahmnicore.model.ClaimCodeGeneration;

import java.util.ArrayList;
import java.util.List;

import static org.bahmni.module.bahmnicore.util.MiscUtils.setUuidsForObservations;

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/bahmnicore/claimcode")
public class ClaimCodeGenerationController extends BaseRestController {

    private BahmniClaimCodeGenerationService bahmniClaimCodeGenerationService;

    public ClaimCodeGenerationController() {
    }

    @Autowired
    public ClaimCodeGenerationController(BahmniClaimCodeGenerationService bahmniClaimCodeGenerationService) {
        this.bahmniClaimCodeGenerationService = bahmniClaimCodeGenerationService;
    }

    @RequestMapping(method = RequestMethod.GET, value = "getClaimCode")
    @ResponseBody
    public String getAutoGenerateClaimCode(){
        return bahmniClaimCodeGenerationService.getAutoGenerateClaimCode();
    }

    @RequestMapping(method = RequestMethod.GET, value = "getMaxClaimCode")
    @ResponseBody
    public String getMaxClaimCode(){
        return bahmniClaimCodeGenerationService.getMaxClaimCode();
    }

    //created API to update current claim code
    @RequestMapping(method = RequestMethod.POST, value = "updateClaimCode")
    @ResponseBody
    public int updateCurrentClaimCode(@RequestParam(value = "claimCode") String claimCode){
        return bahmniClaimCodeGenerationService.updateCurrentClaimCode(claimCode);
    }

    @RequestMapping(method = RequestMethod.GET, value = "summary")
    @ResponseBody
    public ClaimCodeGeneration getAll(@RequestParam(value = "id") String id) {
        ClaimCodeGeneration claimCodeGeneration = bahmniClaimCodeGenerationService.getAll(id);
        return claimCodeGeneration;
    }
}

ClaimCodeGeneration.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.bahmni.module.bahmnicore.model">
    <class name="ClaimCodeGeneration" table="claim_code_generation">

        <id name="id" type="java.lang.Integer" column="id">
            <generator class="native">
                <param name="sequence">claim_code_generation_id_seq</param>
            </generator>
        </id>

        <property name="min_value" type="java.util.String" column="min_value" not-null="true" length="100" />
        <property name="max_value" type="java.util.String" column="max_value" not-null="true" length="100" />
        <property name="current_value" type="java.util.String" column="current_value" not-null="false" length="100" />

    </class>
</hibernate-mapping>

I have added in config.xml

<mappingFiles>
        ClaimCodeGeneration.hbm.xml
        </mappingFiles>

and liquibase.xml

<changeSet id="bahmni-core-20220620001" author="Rojan" runOnChange="true">
        <comment>
            Create Claim Code Generation table
        </comment>
        <createTable tableName="claim_code_generation">
            <column autoIncrement="true" name="id" type="int">
                <constraints primaryKey="true" nullable="false" />
            </column>
            <column name="min_value" type="varchar(100)" />
            <column name="max_value" type="varchar(100)" />
            <column name="current_value" type="char(100)" />
        </createTable>
    </changeSet>

Error I am getting:

{
    "error": {
        "message": "[claim_code_generation is not mapped [select if(current_value is null,max(min_value)+1,max(current_value)+1) from claim_code_generation]]",
        "code": "org.hibernate.hql.internal.ast.QuerySyntaxException:96",
        "detail": "org.hibernate.hql.internal.ast.QuerySyntaxException: claim_code_generation is not mapped [select if(current_value is null,max(min_value)+1,max(current_value)+1) from claim_code_generation]\n\tat org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)\n\tat org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)\n\tat org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)\n\tat org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)\n\tat org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)\n\tat org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)\n\tat org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)\n\tat org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)\n\tat org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)\n\tat org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)\n\tat org.bahmni.module.bahmnicore.dao.impl.ClaimCodeGenerationDaoImpl.getAutoGenerateClaimCode(ClaimCodeGenerationDaoImpl.java:32)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)\n\tat java.lang.reflect.Method.invoke(Unknown Source)\n\tat org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)\n\tat com.sun.proxy.$Proxy269.getAutoGenerateClaimCode(Unknown Source)\n\tat org.bahmni.module.bahmnicore.service.impl.BahmniClaimCodeGenerationServiceImpl.getAutoGenerateClaimCode(BahmniClaimCodeGenerationServiceImpl.java:23)\n\tat org.bahmni.module.bahmnicore.web.v1_0.controller.ClaimCodeGenerationController.getAutoGenerateClaimCode(ClaimCodeGenerationController.java:39)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)\n\tat java.lang.reflect.Method.invoke(Unknown Source)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:618)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:725)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:60)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:72)\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:64)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:105)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\n\tat org.openmrs.module.webservices.rest.web.filter.ContentTypeFilter.doFilter(ContentTypeFilter.java:64)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:82)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\n\tat org.openmrs.module.owa.filter.OwaFilter.doFilter(OwaFilter.java:64)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:54)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:108)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)\n\tat org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)\n\tat org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)\n\tat org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Unknown Source)\nCaused by: org.hibernate.hql.internal.ast.QuerySyntaxException: claim_code_generation is not mapped\n\tat org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)\n\tat org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)\n\tat org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)\n\tat org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:338)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)\n\tat org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)\n\tat org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)\n\tat org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)\n\t... 89 more\n"
    }
}

Any help would be much appreciated.

Any idea on how to solve this problem. @gsluthra

Your DAO uses two different functions: createQuery() and createSQLQuery(). When using createSQLQuery() you’re essentially passing raw SQL to Hibernate to run. When you’re using createQuery() you’re using the Hibernate Query Language (HQL) which is similar to SQL, but not identical. In particular, instead of raw table names, you should use the name of the class, so ClaimCodeGeneration rather than claim_code_generation.

PS You should still use parameterised values with SQL queries. Your code in updateCurrentClaimCode() is vulnerable to an SQL injection.

1 Like

Thanks @ibacher it solved the problem. It was blunder mistake.