2010-10-25 52 views
3

我想嘗試基於字段名Hibernate的異常將ON/WITH條件LEFT JOIN

MNC.java找出所有的fieldValue方法:

private Map <String, String>formFields = new HashMap<String, String>(); 

public void setFieldValue(String fieldName, String fieldValue) { 
     if (formFields == null) { 
      formFields = new HashMap<String, String>(); 
     } 
     formFields.put(fieldName, fieldValue); 

public Map<String, String> getFormFields() { 
     return formFields; 
    } 
    public void setFormFields(Map<String, String> formFields) { 
     this.formFields = formFields; 

public String getFormField(String fieldName) { 
     if ((formFields != null) && (formFields.get(fieldName) != null)) { 
      return (String) formFields.get(fieldName); 
     } 
     return null; 
    } 

MNC.hbm.xml:

<map name="formFields" table="mncformfields" cascade="all-delete-orphan" lazy="true"> 
        <key column="id"/> 
        <index column="fieldName" type="string"/> 
        <element column="fieldValue" type="string"/> 
       </map> 

這裏我的查詢:

query.append("SELECT users.name,sum(mncfield.fieldValue) as TotalMoney"); 
      query.append("from MNC as mnc "); 
      query.append("left join mnc.formFields as mncfield "); 

      query.append("with (mnc.mncID= mncfield.id) and (mnc field.fieldName = 'mapMoney') "); 
      query.append("left join mnc.serviceExec as users "); 
      query.append("where (mnc.mncID= mncfield.id) "); 
      query.append("and mnc.serviceExec.id=users.id "); 
      query.append("and (mnc.mncDate between :startDate and :endDate) "); 
      query.append("and (mnc.mncGroup.id in (:ugIDs)) "); 
      query.append("group by users.name"); 

我得到異常:

org.hibernate.hql.ast.QuerySyntaxException:意外的標記:with`

如果我評論這一行:

//query.append("with (mnc.mnc ID= mncfield.id) and (mnc field.fieldName = 'mapMoney') "); 

然後我得到的異常:

java.lang.IllegalArgumentException:org.hibernate.hql.ast.QuerySyntaxException:with-clause referenced tw Ø不同子句元素

如果我使用的on代替with我得到:

org.hibernate.hql.ast.QuerySyntaxException:意外的標記:在

回答

1

你有沒有請注意,第一行末尾沒有空格(應在TotalMoney之後或之前)?