在org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException:模型类未映射

时间:2017-12-14 14:48:50

标签: spring hibernate spring-boot spring-data-jpa hibernate-mapping

我是SpringBoot的新手。我正在SpringBoot中编写一个项目(以及Hibernate)

访问数据库时,我收到以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: HashMapping is not mapped [from HashMapping where consumerUserId=:consumerUserId]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at com.thp.clinic.tataInsurance.dao.InsuranceDaoImpl.userHashExists(InsuranceDaoImpl.java:24) ~[classes/:na]
    at com.thp.clinic.tataInsurance.dao.InsuranceDaoImpl$$FastClassBySpringCGLIB$$f257a089.invoke(<generated>) [classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) [spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at com.thp.clinic.tataInsurance.dao.InsuranceDaoImpl$$EnhancerBySpringCGLIB$$87c1fe8a.userHashExists(<generated>) [classes/:na]
    at com.thp.clinic.tataInsurance.service.InsuranceServiceImpl.checkInsurance(InsuranceServiceImpl.java:32) [classes/:na]
    at com.thp.clinic.tataInsurance.service.InsuranceServiceImpl$$FastClassBySpringCGLIB$$31bac4e9.invoke(<generated>) [classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) [spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at com.thp.clinic.tataInsurance.service.InsuranceServiceImpl$$EnhancerBySpringCGLIB$$49b68a9f.checkInsurance(<generated>) [classes/:na]
    at com.thp.clinic.tataInsurance.controller.InsuranceController.checkInsurance(InsuranceController.java:31) [classes/:na]

我的application.properties如下:

server.port=8080

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/INSURANCE_DB
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.jpa.show-sql=true
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

#auth  protected here
endpoints.shutdown.sensitive=false

#Enable shutdown endpoint
endpoints.shutdown.enabled=true

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE

我的应用程序配置类如下:

import org.hibernate.SessionFactory;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication()
@ComponentScan(basePackages = { "com.clinic.Insurance" })
public class Application {

    @Bean
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
        return hemf.getSessionFactory();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
道先生如下:

@Override
    public String userHashExists(Long consumerUserId) throws Exception{
        Session session = null;
        try {
            session = factory.openSession();
            Query query = session.createQuery("from HashMapping where consumerUserId=:consumerUserId");
            query.setParameter("consumerUserId", consumerUserId);
            HashMapping mapping = (HashMapping) query.uniqueResult();
            if(mapping!=null)
                return mapping.getConsumerHash();
            return null;
        } catch(Exception e){
            logger.error("Exception in userHashExists Dao", e);
            throw e;
        } finally {
            if(session!=null) {
                session.close();
            }
        }
    }

Model / persistance类如下:

package com.clinic.Insurance.persistance;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "hash_mapping")
public class HashMapping {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "auto_gen")
    @SequenceGenerator(name = "auto_gen", sequenceName = "hash_mapping_hash_mapping_id_seq")
    @Column(name = "hash_mapping_id")
    private Long hashMappingId;

    @Column(name = "consumer_user_id")
    private Long consumerUserId;

    @Column(name = "consumer_hash")
    private String consumerHash;

    @Column(name = "modified_timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Timestamp modifiedTimestamp;

    public Long getHashMappingId() {
        return hashMappingId;
    }

    public void setHashMappingId(Long hashMappingId) {
        this.hashMappingId = hashMappingId;
    }

    public Long getConsumerUserId() {
        return consumerUserId;
    }

    public void setConsumerUserId(Long consumerUserId) {
        this.consumerUserId = consumerUserId;
    }

    public String getConsumerHash() {
        return consumerHash;
    }

    public void setConsumerHash(String consumerHash) {
        this.consumerHash = consumerHash;
    }

    public Timestamp getModifiedTimestamp() {
        return modifiedTimestamp;
    }

    public void setModifiedTimestamp(Timestamp modifiedTimestamp) {
        this.modifiedTimestamp = modifiedTimestamp;
    }

}

有人可以在我出错的地方帮忙。

我是否需要像在spring servelet配置中那样定义类似<hibernate-mapping package="it.besmart.models">的内容。如果是,如何在我现有的代码中执行此操作?

2 个答案:

答案 0 :(得分:0)

回答我出错的地方。

添加@EntityScan(basePackages = { "youentitypackage" })以及@ComponentScan(basePackages = { "com.app" }).这将查找您的实体类。

答案 1 :(得分:0)

我认为这个错误“查询查询= session.createQuery(”来自HashMapping,其中consumerUserId =:consumerUserId“);”当向数据库发出请求时,需要在表WHERE中添加“SELECT”您的实体....  
或者“左”,“HashMapping”到“hash_mapping”(类实体必须与数据库同名)