gpt4 book ai didi

java - 无法在 Hibernate Criteria 中提取 ResultSet

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:08 32 4
gpt4 key购买 nike

在我使用结果 criteria.uniqueResult() 对象 Quote 后出现异常。目的查询 - 从 DB Oracle 的表“quotes”中获取随机结果

org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91) at org.hibernate.loader.Loader.getResultSet(Loader.java:2065) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838) at org.hibernate.loader.Loader.doQuery(Loader.java:909) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) at org.hibernate.loader.Loader.doList(Loader.java:2553) at org.hibernate.loader.Loader.doList(Loader.java:2539) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369) at org.hibernate.loader.Loader.list(Loader.java:2364) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380) at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:402) at ua.com.goit.gojava7.kikstarter.dao.database.QuoteDaoDbImpl.getRandomQuote(QuoteDaoDbImpl.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy48.getRandomQuote(Unknown Source) at ua.com.goit.gojava7.kikstarter.controller.servlet.SelectoinCategories.doGet(SelectoinCategories.java:43) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722)

QuoteDaoDbImpl 类

@Repository
public class QuoteDaoDbImpl implements QuoteDao {

private static final String SELECT_RANDOM_QUOTE = "SELECT * FROM (SELECT * FROM quotes ORDER BY DBMS_RANDOM.VALUE) WHERE rownum = 1";

@Autowired
private SessionFactory sessionFactory;

@Transactional
@Override
public Quote getRandomQuote() {
Session session = sessionFactory.getCurrentSession();

Criteria criteria = session.createCriteria(Quote.class);
criteria.add(Restrictions.sqlRestriction(SELECT_RANDOM_QUOTE));
criteria.setMaxResults(1);

Quote quote = (Quote) criteria.uniqueResult();
session.close();

return quote;
}
}

类选择类别

@WebServlet("/categories")
public class SelectoinCategories extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

@Autowired
private QuoteDao quoteDao;

@Autowired
private CategoryDao categoryDao;

public void init() throws ServletException {

SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());

}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Quote quote = quoteDao.getRandomQuote();
List<Category> categories = categoryDao.getAll();

request.setAttribute("content", quote.getContent());
request.setAttribute("author", quote.getAuthor());
request.setAttribute("categories", categories);
request.getRequestDispatcher("WEB-INF/jsp/categories.jsp").forward(request, response);

}
}

类报价

@Entity
@Table(name = "qoutes")
public class Quote {

@Id
@SequenceGenerator(name = "SEQ_GEN", sequenceName = "seq_id", allocationSize = 10)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
private int id;

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

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

public Quote() {
}

public Quote(String content, String author) {
this.content = content;
this.author = author;
}

public int getId() {
return id;
}

public String getContent() {
return content;
}

public String getAuthor() {
return author;
}

public void setId(int id) {
this.id = id;
}

public void setContent(String quoteContent) {
this.content = quoteContent;
}

public void setAuthor(String quoteAuthor) {
this.author = quoteAuthor;
}

public String toString() {
return "ID: " + id + "; Content: " + content + "; Author: " + author;
}

}

表格报价

CREATE TABLE quotes
(
id INTEGER NOT NULL,
content VARCHAR2(500) NOT NULL,
author VARCHAR2(100) NOT NULL
);

最佳答案

修改尝试

 private static final String SELECT_RANDOM_QUOTE = 
"SELECT id, content, author FROM
(SELECT * FROM quotes ORDER BY DBMS_RANDOM.VALUE) as quotes WHERE rownum = 1";

关于java - 无法在 Hibernate Criteria 中提取 ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077673/

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