1

我正在使用 springframework HibernateTemplate 来查询单个表。使用调试设置,我捕获了 Hibernate 正在生成的查询,在 Toad 中运行它,并获得了 75 个不同的行。但是在我的应用程序中,我得到了一个包含 75 条重复记录的集合,仅此而已。

映射非常简单:

<hibernate-mapping>
<class name="com.p.e.d.s.PmaSummary" <!-- hiding info -->
    table="V_PMA_SUMMARY" schema="ECREDIT">

    <cache usage="read-only" />

    <id column="member_id" name="memberId">
        <generator class="assigned" />
    </id>

    <property name="weekEnding" column="week_ending" />
    <property name="actualInvoice" column="actual_invoice" />
    <property name="ftrAdjustments" column="ftr_adjustments" />
    <property name="edcLseAdjustments" column="edc_lse_adjustments" />
    <property name="blidAdjustments" column="blid_adjustments" />
    <property name="pmaMiscAdjustments" column="pma_misc_adjustments" />
    <property name="pmaEarlyPayments" column="pma_early_payments" />
    <property name="adjInv" column="adj_inv" />
    <property name="adjInvExcEarlyPayment" column="adj_inv_exc_early_payment" />
    <property name="initialPma" column="initial_pma" />
    <property name="threeWeekPma" column="three_week_pma" />
    <property name="pmaOverride" column="pma_override" />
    <property name="pmaOverride_type" column="pma_override_type" />
    <property name="pmaOverride_reason" column="pma_override_reason" />
</class>
</hibernate-mapping>

这个生成的查询在 Toad 中可以正常工作:

select this_.member_id as member1_45_0_,
     this_.week_ending as week2_45_0_,
     this_.actual_invoice as actual3_45_0_,
     this_.ftr_adjustments as ftr4_45_0_,
     this_.edc_lse_adjustments as edc5_45_0_,
     this_.blid_adjustments as blid6_45_0_,
     this_.pma_misc_adjustments as pma7_45_0_,
     this_.pma_early_payments as pma8_45_0_,
     this_.adj_inv as adj9_45_0_,
     this_.adj_inv_exc_early_payment as adj10_45_0_,
     this_.initial_pma as initial11_45_0_,
     this_.three_week_pma as three12_45_0_,
     this_.pma_override as pma13_45_0_,
     this_.pma_override_type as pma14_45_0_,
     this_.pma_override_reason as pma15_45_0_
from ecredit.v_pma_summary this_
where this_.member_id = 10003
order by this_.week_ending desc;
4

2 回答 2

1

为什么不添加DISTINCT到您的 HQL 查询?

于 2012-05-31T12:08:43.260 回答
0

映射文件中的 id 定义不正确。随着<ìd>您将单个列定义为主键。

在选择中,您选择一个 member_id 的所有行并按 week_ending 对它们进行排序。所以我想对于同一个member_id,可能会有很多列具有不同的week_ending。所以你的主键可能有两列或更多列,member_id 和 week_ending,也许还有……。别的。

你必须<composite-id>改用<id>,sth。像这样

<composite-id>
    <key-property name="memberId" column="member_id" type="..." /> 
    <key-property name="weekEnding" column="week_ending" type="..." />
    <!--- perhaps more key-properties --> 
</composite-id>
于 2012-05-31T12:26:49.737 回答