如何使用Hibernate获取只有一列的值

时间:2013-05-04 00:12:59

标签: java mysql hibernate

我刚开始使用Hibernate进行开发,我在尝试选择表格中的列时遇到了问题。我有一个表有一个名为“nome”的列,它是一个字符串。我想要做的就是在表格中获取此列的所有值。为此,我创建了方法:

public List<String> obtemNomePorQuery(String query) {

        EntityManager manager = getEntityManager();

        Session session = manager.unwrap(Session.class);    

        List<String> listResult = (List<String>) session.createQuery(query).list();
        manager.close();

        return listResult;
    }

在这种情况下,我在查询参数中传递“从lojas中选择nome”。 在MySQL中,我可以运行此查询,它返回我想要的一切。使用Hibernate我得到以下日志:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: lojas is not mapped [select nome from lojas]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: lojas is not mapped [select nome from lojas]
    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:326)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
    org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
    org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
    org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
    org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
    com.puccamp.projetofinal.persistence.Dao.obtemNomePorQuery(Dao.java:198)
    com.puccamp.projetofinal.controller.LojaController.gerenciaLoja(LojaController.java:63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我可以做错什么?

1 个答案:

答案 0 :(得分:1)

您使用的方法是期望HQL,而不是SQL。是否有与lojas表关联的持久对象?如果不是HQL不是您的选项,您将需要获得与数据库的本机连接以执行SQL。您是否尝试过createSQLQuery(String queryString) Session方法?这是javadoc:http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html