Hibernate exception : table is not mapped

I have added new table doctor in openmrs database. And then created Doctor.java

import org.codehaus.jackson.annotate.JsonIgnore;
import org.openmrs.Auditable;
import org.openmrs.BaseOpenmrsObject;
import org.openmrs.User;

import java.io.Serializable;
import java.util.Date;

public class Doctor extends BaseOpenmrsObject implements Auditable, Serializable {
    private Integer doctorId;

    private String name;

    private String spelization;

    private Double doctorCharge;

    private User creator;

    private Date dateCreated;

    private User changedBy;

    private Date dateChanged;

    public Integer getDoctorId() {
        return doctorId;
    }

    public void setDoctorId(Integer doctorId) {
        this.doctorId = doctorId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpelization() {
        return spelization;
    }

    public void setSpelization(String spelization) {
        this.spelization = spelization;
    }

    public Double getDoctorCharge() {
        return doctorCharge;
    }

    public void setDoctorCharge(Double doctorCharge) {
        this.doctorCharge = doctorCharge;
    }

    @Override
    public Integer getId() {
        return doctorId;
    }

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

    @Override
    @JsonIgnore
    public User getCreator() {
        return creator;
    }

    @Override
    public void setCreator(User creator) {
        this.creator = creator;
    }

    @Override
    public Date getDateCreated() {
        return dateCreated;
    }

    @Override
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    @Override
    @JsonIgnore
    public User getChangedBy() {
        return changedBy;
    }

    @Override
    public void setChangedBy(User changedBy) {
        this.changedBy = changedBy;
    }

    @Override
    public Date getDateChanged() {
        return dateChanged;
    }

    @Override
    public void setDateChanged(Date dateChanged) {
        this.dateChanged = dateChanged;
    }
}

DoctorDaoImpl.java

import org.apache.commons.collections.CollectionUtils;
import org.bahmni.module.admin.config.dao.DoctorDao;
import org.bahmni.module.admin.config.model.Doctor;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class DoctorDaoImpl implements DoctorDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Doctor getDoctor(int id) {
        List<Doctor> doctorList = new ArrayList<>();
        Session currentSession = sessionFactory.getCurrentSession();
        Query query = currentSession.createQuery("select d from Doctor d " + " where d.doctorId = :id ");
        query.setParameter("id", id);
        doctorList.addAll(query.list());
        return CollectionUtils.isEmpty(doctorList) ? null : doctorList.get(0);
    }
}

DoctorServiceImpl.java

import org.bahmni.module.admin.config.dao.DoctorDao;
import org.bahmni.module.admin.config.model.Doctor;
import org.bahmni.module.admin.config.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorDao doctorDao;

    @Override
    public Doctor getInfo(Integer id) {
        return doctorDao.getDoctor(id);
    }
}

Doctor.hbm.xml and aded under resources

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="org.bahmni.module.admin.config.model">

    <class name="Doctor" table="doctor">

        <id name="doctorId" type="int" column="doctor_id">
            <generator class="native">
                <param name="sequence">doctor_id_seq</param>
            </generator>
        </id>
        <property name="uuid" type="java.lang.String" column="uuid" length="38" unique="true"/>
        <property name="dateCreated" type="java.util.Date" column="date_created" not-null="true" length="19"/>

        <property name="dateChanged" type="java.util.Date" column="date_changed" not-null="false" length="19"/>

        <many-to-one name="changedBy" class="org.openmrs.User" column="changed_by"/>
        <many-to-one name="creator" class="org.openmrs.User" not-null="true"/>

        <property name="name" type="java.lang.String" column="name" not-null="true"/>
        <property name="spelization" type="java.lang.String" column="spelization" not-null="false"/>
        <property name="doctorCharge" type="java.lang.Double" not-null="true" column="doctor_charge"/>
    </class>
</hibernate-mapping>

Added mapping resource in test-hibernate.cfg.xml

<mapping resource="Doctor.hbm.xml"/>

Added mappingFiles Doctor.hbm.xml in config.xml

<mappingFiles>
    Doctor.hbm.xml
</mappingFiles>

But when I call doctorService.getInfo(1); from controller it throw Exception

22-02-2017 13:28:10 [ERROR] BahmniConfigController - Doctor is not mapped [select d from Doctor d  where d.configId = :id ]
org.hibernate.hql.internal.ast.QuerySyntaxException: Doctor is not mapped [select d from Doctor d  where d.configId = :id ]
        at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
        at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
        at org.bahmni.module.admin.config.dao.impl.DoctorImpl.getDoctor(DoctorImpl.java:28)
        at org.bahmni.module.admin.config.service.impl.DoctorServiceImpl.getInfo(DoctorServiceImpl.java:20)
        at org.bahmni.module.bahmnicore.web.v1_0.controller.BahmniConfigController.test(BahmniConfigController.java:69)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:60)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:72)
        at org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:64)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)
        at org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:108)
        at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)
        at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:82)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)
        at org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:54)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:108)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
        at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Doctor is not mapped
        at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
        at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
        at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
        at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:338)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
        ... 78 more

How I can resolve this issue.

not sure, but its interesting that in the exception it says configId and not doctorId which you show us in your DoctorDaoImpl

You can simplify your getDoctor implementation in DoctorDaoImpl with

(Doctor) sessionFactory.getCurrentSession().get(Doctor.class, id);

I know it says Doctor is not mapped but its weird that it shows a different query than what you have in your DAOImpl. Can you please try that.

Another thing, is that error happening in a test or when you deploy your module? It could also be that you are not copying the hbm.xml in your build process into the final omod file. But I doubt thats it because I think the error message would then be that the mapping file you define in the config.xml is not found.

Im sure will find the solution :slight_smile:

Hello , I am also experiencing a similar issue when I am trying to use org.openmrs.User. Any solution arrived please share.

Thanks

HI @buvaneswariarun , can you elaborate on how your using the User ? is that in a test case or in the actual deplyment ??

I have a field in my table ‘creator’, where we store the creator details. It is an instance of org.openmrs.User - which I believed will be automatically done when we do a .saveOrUpdate but I get this error instead.

are you using Annotations or XML based mappings ?? @buvaneswariarun , it could be better if you provided the code snippets of your Class , your Hibernate mappings and the actual error logs