如何从一对多单向关系中获取 "many" 部分中的对象?

How do I get an object in the "many" part from a One-To-Many unidirectional relationship?

我不知道如何准确表达我正在寻找的东西,所以我会解释我有什么,然后解释我想做什么。

我有一个 Class 模型,它有 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
}

实体Class就像一个主键标准,我们称它为"OID design pattern",但不知道英文是不是这样。

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 和来自 TaxTypeAttribute 的 attributeName,但我需要获取完整的 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:代码有效。我将它用于其他查询和工作,它只是不适用于这个,或者我不知道如何让它工作。可能你不能做那种搜索,我不知道。

从 HQL/JPQL 的角度来看,您可以将查询写成:

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

此查询将 return 您 TaxTypeAttribute 个符合指定条件的实例。你如何将其翻译成你的查询语言是我无能为力的事情。