簡體   English   中英

如何對oracle進行條件查詢

[英]How to do criteria queries for oracle

如何重新編寫此代碼以與 oracle 一起使用。 在此之前,它與 Postgres 一起工作並且沒有錯誤。 現在它給出了sql語法錯誤。

 public List<MyOrder> myOrderListNew(Company company){
        Criteria criteria = session.getCurrentSession().createCriteria(MyOrder.class);
        criteria.add(Restrictions.eq("company.id", company.getId()));
        criteria.add(Restrictions.eq("removeorder", false));
        criteria.add(Restrictions.eq("status", "new"));
        criteria.addOrder(Order.desc("id"));
        List<MyOrder> myOrders = criteria.list();
        return myOrders;
    }

關於: criteria.add (Restrictions.eq ("company.id", company.getId ()));

“company.id”,這里是實體類 MyOrder 中的此屬性的company

堆棧跟蹤

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)




java.sql.SQLSyntaxErrorException: ORA-01747: invalid user.table.column, table.column, or column specification

    oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
    oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
    oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
    oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
    oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:252)



Error : 1747, Position : 144, Sql = select this_.myOrder_id as myOrder_id1_6_0_, this_.myorder_company_id as myorder_company_id9_6_0_, this_.courier_id as courier_id10_6_0_, this_.date as date2_6_0_, this_.date_hms as date_hms3_6_0_, this_.hide as hide4_6_0_, this_.removeorder as removeorder5_6_0_, this_.selected as selected6_6_0_, this_.shops_id as shops_id11_6_0_, this_.status as status7_6_0_, this_.sum as sum8_6_0_ from myorder this_ where this_.myorder_company_id=:1  and this_.removeorder=:2  and this_.status=:3  order by this_.myOrder_id desc, OriginalSql = select this_.myOrder_id as myOrder_id1_6_0_, this_.myorder_company_id as myorder_company_id9_6_0_, this_.courier_id as courier_id10_6_0_, this_.date as date2_6_0_, this_.date_hms as date_hms3_6_0_, this_.hide as hide4_6_0_, this_.removeorder as removeorder5_6_0_, this_.selected as selected6_6_0_, this_.shops_id as shops_id11_6_0_, this_.status as status7_6_0_, this_.sum as sum8_6_0_ from myorder this_ where this_.myorder_company_id=? and this_.removeorder=? and this_.status=? order by this_.myOrder_id desc, Error Msg = ORA-01747: invalid user.table.column, table.column, or column specification

    oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:498)
    oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
    oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
    oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
    oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:252)
    oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:612)

格式化后,您使用的查詢是這樣的。 我已經標記了似乎是錯誤的:列名不能是DATESUM因為它們是保留字(對於數據類型和函數)。

  SELECT this_.myOrder_id AS myOrder_id1_6_0_,
         this_.myorder_company_id AS myorder_company_id9_6_0_,
         this_.courier_id AS courier_id10_6_0_,
         this_.date AS date2_6_0_,                               --> this
         this_.date_hms AS date_hms3_6_0_,
         this_.hide AS hide4_6_0_,
         this_.removeorder AS removeorder5_6_0_,
         this_.selected AS selected6_6_0_,
         this_.shops_id AS shops_id11_6_0_,
         this_.status AS status7_6_0_,
         this_.SUM AS sum8_6_0_                                  --> this
    FROM myorder this_
   WHERE     this_.myorder_company_id = :1
         AND this_.removeorder = :2
         AND this_.status = :3
ORDER BY this_.myOrder_id DESC

該怎么辦? 取決於表描述。 如果您設法創建了這樣的表,那么您必須將列名括在雙引號中。 如果是這樣,則每次處理這些列時都必須執行相同的操作,例如

SQL> create table myorder
  2    ("DATE"   date,
  3     "SUM"    number
  4    );

Table created.

SQL> -- this is what you are currently doing; see? The same ORA-01747 error
SQL> select this_.date,
  2         this_.sum
  3  from myorder this_;
select this_.date,
             *
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification

SQL> -- this is what you should be doing
SQL> select this_."DATE",
  2         this_."SUM"
  3  from myorder this_;

no rows selected

SQL>

如果可能,請更改列名稱以使您的生活更輕松。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM