无法在Hibernate Criteria

时间:2016-01-29 05:42:58

标签: java oracle hibernate resultset

我在适当的结果criteria.uniqueResult()对象引用后得到了异常。目的查询 - 从表格中获取随机结果'引号'来自DB Oracle

  

org.hibernate.exception.SQLGrammarException:无法提取ResultSet       在org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)       在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)       在org.hibernate.loader.Loader.getResultSet(Loader.java:2065)       在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)       在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)       在org.hibernate.loader.Loader.doQuery(Loader.java:909)       在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)       在org.hibernate.loader.Loader.doList(Loader.java:2553)       在org.hibernate.loader.Loader.doList(Loader.java:2539)       在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)       在org.hibernate.loader.Loader.list(Loader.java:2364)       在org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)       在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)       在org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)       在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)       在java.lang.reflect.Method.invoke(Method.java:601)       在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)       在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)       在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)       在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)       在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)       在com.sun.proxy。$ Proxy48.getRandomQuote(未知来源)       在ua.com.goit.gojava7.kikstarter.controller.servlet.SelectoinCategories.doGet(SelectoinCategories.java:43)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)       在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)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       在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;
    }
}

类SelectoinCategories

@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
);

1 个答案:

答案 0 :(得分:0)

修改尝试

 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";