【问题标题】:How do I get an object in the "many" part from a One-To-Many unidirectional relationship?如何从一对多单向关系中获取“多”部分中的对象?
【发布时间】:2017-02-09 04:31:23
【问题描述】:

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

我有一个 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
}

Entity Class就像一个主键标准,我们称之为“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 和来自 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:代码有效。我将它用于其他查询和工作,它只是不适用于这个,或者我不知道如何使它工作。可能你不能做那种搜索,我不知道。

【问题讨论】:

  • 看起来映射存在问题。重新检查 pojo 以及列名和表名。还将您的代码发布到您触发查询的位置。
  • 映射很好,至少它适用于所有其他 Criteria 查询。这是唯一不工作的。我正在编辑帖子以添加查询

标签: java xml hibernate one-to-many hibernate-onetomany


【解决方案1】:

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

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

此查询将为您返回符合指定条件的TaxTypeAttribute 实例。你如何将它翻译成你的查询语言是我无法帮助的。

【讨论】:

  • 我试过了,它可以工作......遗憾的是,我们不允许使用 JOIN,但它似乎是唯一的方法。我会和我的老师谈谈。谢谢你!
  • 你可以使用什么?也许是反向引用关联?
猜你喜欢
  • 2013-11-16
  • 1970-01-01
  • 2016-07-20
  • 2015-04-12
  • 2020-02-05
  • 2014-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
相关资源
最近更新 更多