【问题标题】:RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute queryRuntimeException:org.hibernate.exception.SQLGrammarException:无法执行查询
【发布时间】:2017-02-23 12:12:25
【问题描述】:

我正在使用另一个表连接从名为 PurchaseDetailContributor 的表中检索记录。但我收到查询错误。

下面是我的 DAOHibernate.java 类,我写了一个方法。

@SuppressWarnings("unchecked")
public List<PurchaseDetailContributor> getPurchaseDetailContributorByCustomerNo(Integer customer_no) {
    System.out.println("The Customer number in PurchaseDetailContributorDAOHIbernate >>>>" + customer_no);
    String sql = " SELECT * FROM purchase_detail_contributor pdc "
             + " INNER JOIN purchasedetail pd ON pdc.purchase_detail_id = pd.purchasedetailid "
             + " INNER JOIN purchase p ON pd.purchasefk = p.purchaseid " 
             + " INNER JOIN user u ON u.userid = p.userid AND u.customer_no = " +customer_no;

    List<PurchaseDetailContributor> pdcList = new ArrayList<PurchaseDetailContributor>();
    Transaction tx = null;
    Session session = getHibernateTemplate().getSessionFactory().openSession();
    try{
        tx = session.beginTransaction();
        SQLQuery sqlQuery = session.createSQLQuery(sql).addEntity(PurchaseDetailContributor.class);
        //sqlQuery.setInteger("customer_no", customer_no);
        pdcList = sqlQuery.list();
        tx.commit();
    }catch (RuntimeException e) {
        System.out.println("getPurchaseDetailContributorByCustomerNo() >>>>- RuntimeException: " + e);
        if (tx != null && tx.isActive()) {
            try {
                // Second try catch as the rollback could fail as well
                tx.rollback();
            } catch (HibernateException e1) {
                System.out.println("Error rolling back transaction: " + e1);
            }
        }
    } finally {
        session.disconnect();
    }

我的错误是在连接查询中。

//错误信息

1:13:00,713 DEBUG [UserDAOHibernate:774] findUsersByCustomer_no() - Namequery: GetUsersByCustomer_no, 57835
11:13:00,752 DEBUG [UserDAOHibernate:774] findUsersByCustomer_no() - Namequery: GetUsersByCustomer_no, 57835
This COMPANYID is before passing from  FORM >>>>>>>>41
This COMPANYtype after passing id from FORM >>>>>>>>UnTrusted
The customer number is >>>>>>57835
User select values is Untrusted
Inside try condition
The Customer number in PurchaseDetailContributorDAOHIbernate *********57835
11:13:01,073 WARN  [JDBCExceptionReporter:233] SQL Error: 0, SQLState: S0022
11:13:01,074 ERROR [JDBCExceptionReporter:234] Column 'is_approved' not found.
getPurchaseDetailContributorByCustomerNo() ====222>>>>- RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute query
java.lang.NullPointerException
at com.newscom.action.AccountAdminAction.updateTheCustomer(AccountAdminAction.java:4749)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)

【问题讨论】:

  • 在添加“inner join”和“where”子句时需要一些空格,否则你的 sql 将类似于“select * from purchase_detail_contributorinner join...”。
  • @Insac & @stanislavL,谢谢现在我按照你们的建议更改了 sql 查询,但我仍然收到以下错误 org.hibernate.exception.SQLGrammarException: could not execute query
  • 您可以在提交之前打印您的 SQL 并将其添加到问题中吗?
  • 我没听懂您的意思...请您修改我的代码中的错误。
  • 为了帮助我们了解可能出现的问题,您能补充一些信息吗?例如完整的堆栈跟踪和/或 sql 变量的值?

标签: java mysql sql hibernate


【解决方案1】:

你的 sql 中没有空格

String sql = "select * from purchase_detail_contributor"
         + "inner join purchasedetail on (purchase_detail_contributor.purchase_detail_id = purchasedetail.purchasedetailid)"
         + "inner join purchase on (purchasedetail.purchasefk = purchase.purchaseid)" 
         + "inner join user on (user.userid = purchase.userid)"
         + "where user.customer_no = :customer_no";

所以sql是

select * from purchase_detail_contributorinner join purchasedetail...

这绝对是语法错误

【讨论】:

  • 创建查询时必须始终设置user.customer_no = :cno等参数,然后调用setParameter("cno", customer_no)来防止SQL注入攻击。这个更安全,因为它是一个 Integer 对象,但如果你在一个地方这样做,可以安全地假设你在做更多的事情。
【解决方案2】:

您发布的代码是否正确?

显示错误

11:13:01,074 ERROR [JDBCExceptionReporter:234] Column 'is_approved' not found.

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2016-08-30
    • 2015-10-06
    • 2017-09-28
    • 2023-03-03
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多