2017-02-09 53 views
0

我不知道如何正確表達我在找什麼,所以我會解釋我有什麼,然後解釋我想要做什麼。如何從「一對多」單向關係中的「多」部分獲取對象?

我有一個類模型,它有2個類與一對多單向關係。

public class TaxType extends Entity implements java.io.Serializable { 
    //stuff 
    private Set<TaxTypeAttribute> listTaxTypeAttribute = new HashSet<>(0); 
} 
public class TaxTypeAttribute extends Entity implements java.io.Serializable { 
    private String attributeName; 
    //stuff, but no reference to TaxType 
} 

實體類是像一個主鍵的標準,我們稱之爲「OID設計模式」,但如果它是不知道一樣,在英語。

public class Entity { 
    private String oid; 
    //constructor, get and set 
} 

在映射,它是這樣的:

<class name="entity.TaxType" table="taxttype" catalog="tax_type" optimistic-lock="version"> 
    <id name="oid" type="string"> 
     <column name="OIDtt" length="50" /> 
     <generator class="uuid2" /> 
    </id>   
    <set name="listAtributoTipoImpuesto"> 
     <key column="OIDtt" not-null="true"/> 
     <one-to-many class="entidades.AtributoTipoImpuesto" /> 
    </set> 
</class> 
<!-- two separated files, this is just for showing --> 
<class name="entity.TaxTypeAttribute" table="taxtypeattribute" catalog="tax_type" optimistic-lock="version"> 
    <id name="oid" type="string"> 
     <column name="OIDtta" length="50" /> 
     <generator class="uuid2" /> 
    </id> 
    <property name="attributeName" type="string"> 
     <column name="attributeName" length="50" not-null="true" /> 
    </property> 
</class> 

在程序的一個步驟,我有一個TaxType和attributeName從TaxTypeAttribute,但我需要得到充分TaxTypeAttribute。我正在通過Criteria API進行查詢。我可以做taxType.getListTaxTypeAttribute();並做一個循環,直到找到對象,但我想知道是否有一種方法可以使用一些Hibernate查詢。

我試着做taxType.getOid();,然後使用和attributeName但它拋出一個異常:

Exception in thread "main" org.hibernate.QueryException: could not resolve property: OIDtt of: entity.TaxTypeAttribute 

任何線索?謝謝你,請原諒我的英文不好

編輯:爲了遵循設計模式,我們使用這種方法來做SELECT查詢:Awful thing we use for querys。 我做它的方式是這樣的:

ArrayList<DTOCriteria> criteriaList = new ArrayList<>(); 
DTOCriteria c1 = new DTOCriteria(); 
c1.setAttribute("OIDtt"); 
c1.setOperation("="); 
c1.setValue(taxType.getOID()); 
criteriaList.add(c1); 
ArrayList<Object> found = search("TaxTypeAttribute"); 

我可以添加其他DTOCriteria如果我想(「的attributeName」;「=」;的attributeName,例如),但如果前者沒有工作它是一種無用。我也嘗試過(僅僅因爲它是免費的),使用「TaxType」作爲屬性,TaxType對象作爲值,但也沒有工作。

PS:代碼有效。我將它用於其他查詢和作品,它只是對這個不起作用,或者我不知道如何使它工作。可能是你不能做那種搜索,我不知道。

+0

看起來像是映射有問題。請檢查pojo的列名和表名。也可以將您的代碼發佈到您激活查詢的位置。 – Akshay

+0

映射沒問題,至少它適用於其他Criteria查詢。這是唯一不能工作的人。我正在編輯帖子來添加查詢 – fkchaud

回答

0

從HQL/JPQL的角度來看,你可以寫你的查詢爲:

SELECT tta FROM TaxType tt JOIN tt.listTaxTypeAttribute tta 
WHERE tt.oid = :oid 
    AND tta.attributeName = :attributeName 

此查詢將返回符合指定條件的TaxTypeAttribute實例。你如何將它翻譯成你的查詢語言是我無法協助的。

+0

我試過了,它的工作原理......可悲的是,我們不允許使用JOIN,但它似乎是唯一的方法。我會告訴我的老師。謝謝! – fkchaud

+0

你允許使用什麼?也許是後面的參考協會? – Naros

相關問題