2017-02-09 95 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

相关问题