将ON / WITH条件应用于LEFT JOIN的Hibernate异常

时间:2010-10-25 06:58:41

标签: java hibernate orm hql

我想尝试根据fieldName找出所有的fieldValue

MNC.java:

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引用了两个不同的from子句元素

如果我使用on代替with,我会:

  

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:on

1 个答案:

答案 0 :(得分:1)

您是否注意到第一行末尾没有空格(应该是在TotalMoney之后还是之后)?