2015-11-02 40 views
1

我有2個表有一個一對多的關係,按如下:HQL一個一對多的關係,讓「ORA-00936:缺少表達式」異常

Table Relationship

我的實體類和HBM配置是按以下:

AuditDetail.java

public class AuditDetail implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private String auditId; 
    private Timestamp startTime; 
    private Timestamp endTime; 
    Set auditParameterDataSet; 

    public Set getAuditParameterDataSet() { 
     return auditParameterDataSet; 
    } 
    public void setAuditParameterDataSet(Set auditParameterDataSet) { 
     this.auditParameterDataSet = auditParameterDataSet; 
    } 
    public String getAuditId() { 
     return auditId; 
    } 
    public void setAuditId(String auditId) { 
     this.auditId = auditId; 
    } 
    public void setStartTime(Timestamp startTime) { 
     this.startTime = startTime; 
    } 
    public Timestamp getEndTime() { 
     return endTime;   
    } 
    public void setEndTime(Timestamp endTime) { 
     this.endTime = endTime; 
    } 
} 

AuditDetail.hbm.xml

<hibernate-mapping> 

    <class name="com.test.AuditDetail" table="AUDITDETAIL" >   

     <id name="auditId" column="AUDITID" type="string" /> 

     <property name="startTime" type="java.sql.Timestamp"> 
       <column name="STARTTIME" sql-type="VARCHAR2(50)" /> 
     </property> 

     <property name="endTime" type="java.sql.Timestamp"> 
       <column name="ENDTIME" sql-type="VARCHAR2(50)" /> 
     </property> 

     <set name="auditParameterDataSet" cascade="all,delete-orphan" lazy="true" inverse="true"> 
      <key> 
       <column name="AUDITID"/> 
      </key> 
      <one-to-many class="com.test.AuditParameter" /> 
     </set> 

    </class> 

</hibernate-mapping> 

AuditParameter.java

public class AuditParameter { 

    private String auditId ; 
    private String parameterId; 
    private String fieldName ; 
    private AuditDetail auditDetail; 

    public String getAuditId() { 
     return auditId; 
    } 
    public void setAuditId(String auditId) { 
     this.auditId = auditId; 
    } 
    public String getParameterId() { 
     return parameterId; 
    } 
    public void setParameterId(String parameterId) { 
     this.parameterId = parameterId; 
    } 
    public String getFieldName() { 
     return fieldName; 
    } 
    public void setFieldName(String fieldName) { 
     this.fieldName = fieldName; 
    } 
    public AuditDetail auditDetail() { 
     return auditDetail; 
    } 
    public void setAuditDetail(AuditDetail auditDetail) { 
     this.auditDetail = auditDetail; 
    } 

} 

AuditParameter.hbm.xml

<hibernate-mapping> 

    <class name="com.test.AuditParameter" table="AUDITPARAMETER" >   

     <id name="parameterId" type="string"> 
       <column name="PARAMETERID" sql-type="VARCHAR2(50)" /> 
     </id> 

     <many-to-one name="auditDetail" class="com.test.AuditDetail" column="AUDITID" insert="false" update="false" lazy="false"> 
     </many-to-one> 

     <property name="auditId" type="string"> 
       <column name="AUDITID" sql-type="VARCHAR2(50)" not-null="false" /> 
     </property> 

     <property name="fieldName" type="string"> 
       <column name="FIELDNAME" sql-type="VARCHAR2(50)" /> 
     </property> 


    </class> 
</hibernate-mapping> 

我的HQL查詢是每如下:

select a.auditId, a.startTime, a.endTime, a.auditParameterDataSet from AuditDetail a; 

但是,當我執行這個查詢,它給了我以下錯誤:

ORA-00936: missing expression

以下是查詢我的控制檯輸出解僱。

select au0_.AUDITID as col_0_0_, au0_.STARTTIME as col_1_0_, au0_.ENDTIME as col_2_0_, . as col_3_0_ 
from AUDITDETAIL au0_ inner join AUDITPARAMETER auditparam4_ on au0_.AUDITID=auditparam4_.AUDITID 

根據這個輸出我們可以看到hibernate沒有爲a.auditParameterDataSet生成列名。爲什麼發生這種情況?我在哪裏做錯了?

回答

3

你必須使用join: -

HQL: -

select a.auditId, a.startTime, a.endTime, auditParameterDataSet from AuditDetail join a.auditParameterDataSet auditParameterDataSet; 

標準: -

Criteria criteria = session.createCriteria(AuditDetail.class); 
criteria.setFetchMode("auditParameterDataSet", FetchMode.EAGER); 
+1

謝謝您的回答,把 「加盟」 解決我的問題。我所做的唯一更改是在查詢中用auditParameterDataSet替換a.auditParameterDataSet,並從查詢中刪除** fetch **,因爲它給了我如下錯誤:_query指定了聯接抓取,但抓取的關聯的所有者不存在於選擇list._請參考[此鏈接](http://stackoverflow.com/questions/12459779/query-specified-join-fetching-but-the-owner-of-the-fetched-association-was-不),並更新你的答案。 BTW +1從我的最終解決我的問題。 –