Hibernate语法异常表未映射

时间:2015-01-20 19:56:28

标签: java spring hibernate model-view-controller

我一直遇到以下错误 - 请求处理失败;嵌套异常是org.hibernate.hql.internal.ast.QuerySyntaxException:强制未映射[来自强制执行]。看看这个网站上的其他问题,似乎是错误地映射类型的问题,但我不相信这是因为我在HQL中使用“来自执行”而我的班级是执行。

Enforcement.java:

package testspringmvc.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity

@Table(name="enforcement")
public class Enforcement extends Element{
    @Id
    @Column(name="ENFORCEMENT_CD")
    private String Id;

    @Column(name="ENFORCEMENT_DESC")
    private String name;

    public String getId() {
        return Id;
    }

    public void setId(String Id) {
        this.Id = Id;
    }

    public String getName() {
        return name;
    }

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

MemberController.java

public class MemberController {
    @Autowired
    private RepositoryServices repositoryService;


    private FilterOption filterOption;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView loadFormPage() {
        List<Document> repositoryList = new ArrayList<Document>();

        // Create and add lists and objects to the model.
        ModelAndView model = new ModelAndView("search");
        model.addObject("document", new Document());
        model.addObject("filter", new FilterOption());
        model.addObject("enforcementList", repositoryService.getEnforcementList());
        model.addObject("repositoryList", repositoryList);
        model.addObject("repositorySize", repositoryList.size());
        return model;
    }
}

RepositoryDaoImpl.java:

@Repository
public class RepositoryDaoImpl implements RepositoryDao {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public List<Enforcement> getEnforcementList() {
        Session session = sessionFactory.openSession();
        @SuppressWarnings("unchecked")
        List<Enforcement> enforcementList = session.createQuery("from Enforcement").list();
        System.out.println(enforcementList);
        session.close();
        return enforcementList;
    }

}

RepositoryServicesImpl.java:

@Service
public class RepositoryServicesImpl implements RepositoryServices 
{  

    @Autowired  
    RepositoryDao repodao;  

    @Override
    public List<Enforcement> getEnforcementList() {
        return repodao.getEnforcementList();
    }
} 

堆栈追踪:

JBWEB000070: exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Enforcement is not mapped [from Enforcement]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
JBWEB000071: root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: Enforcement is not mapped [from Enforcement]
    org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
    org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:250)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
    org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
    org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1784)
    gaimsspringmvc.repo.RepositoryDaoImpl.getEnforcementList(RepositoryDaoImpl.java:52)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
    com.sun.proxy.$Proxy34.getEnforcementList(Unknown Source)
    gaimsspringmvc.services.RepositoryServicesImpl.getEnforcementList(RepositoryServicesImpl.java:56)
    gaimsspringmvc.mvc.MemberController.loadFormPage(MemberController.java:79)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 

2 个答案:

答案 0 :(得分:0)

可能是一个区分大小写的问题?试试@Table(name="Enforcement")

答案 1 :(得分:0)

仅供参考,在sessionFactory bean中添加类名作为annotatedClasses列表中的值可以解决我的困境。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>testspringmvc.domain.Enforcement</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show.sql">true</prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/test</prop>
                <prop key="hibernate.connection.username">root</prop>
                <prop key="hibernate.connection.password">gaims</prop>
                <prop key="hibernate.connection.pool_size">1</prop>
                <!-- <prop key="hibernate.hbm2dll.auto">validate</prop> -->
            </props>
        </property>

    </bean>