gpt4 book ai didi

java - 如何使用 Hibernate 仅获取一列的值

转载 作者:行者123 更新时间:2023-11-29 13:53:03 24 4
gpt4 key购买 nike

我刚刚开始使用 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;
}

在这种情况下,我在查询参数中传递“select nome from lojas”。在 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)

我可能做错了什么?

最佳答案

您使用的方法需要 HQL,而不是 SQL。是否存在与 lojas 表关联的持久对象?如果不是,HQL 不适合您,您将需要获得与数据库的 native 连接来执行 SQL。您是否尝试过 SessioncreateSQLQuery(String queryString) 方法?这是javadoc:http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/Session.html

关于java - 如何使用 Hibernate 仅获取一列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com