(n+1) 选择我无法解决的问题。我正在加入具有非 PK 列的表。
我的架构
记录
Record_Id (PK)
Carrier_Number
Audit_Details
AuditId(PK)
Carrier_Number
Records 和 Audit_Details 之间存在一对多的关系。
我的记录.hbm.xml
<set lazy="true" name="auditDetails" sort="unsorted"
table="AUDIT_DETAILS" inverse="true">
<key column="Carrier_Number" not-null="true" property-ref="carrierRefNumber"/>
<one-to-many
class="com.package.AuditDtls" />
</set>
我的 auditDetails.hbm.xml
<many-to-one
class="com.package.Records" fetch="join"
name="Records" column="Carrier_Number" not-null="true" property-ref="carrierNumber" lazy="false"/>
这会产生一个类似的查询
select
this_.CARRIER_NUMBER as CARRIER1_2_2_,
abc1_.CARRIER_NUMBER as CARRIER8_3_0_,
otm4_.CARRIER_NUMBER as CARRIER1_2_1_,
from
RECORDS this_
inner join
AUDIT_DETAILS abc1_
on this_.CARRIER_NUMBER=abc_.CARRIER_NUMBER
left outer join
RECORDS otm4_
on abc1_.CARRIER_NUMBER=otmp4_.CARRIER_NUMBER
where
this_.LOAD_ID=?
select
auditde0_.CARRIER_NUMBER as CARRIER8_1_
from
AUDIT_DTLS auditde0_
where
auditde0_.CARRIER_NUMBER=?
我尝试更改为 fetch="select" ,更改lazy="false" 和lazy="no-proxy" 但到目前为止没有任何效果。我不确定这个问题是否是因为加入了两个带有 nonPK 列的表。将不胜感激任何建议。