繁体   English   中英

使用非PK连接进行休眠(n + 1)选择

[英]Hibernate (n+1) select with non-PK join

(n + 1)个我无法解决的问题。 我正在用非PK列连接表。

我的架构

记录
Record_Id(PK)
承运人编号


Audit_Details
AuditId(PK)
承运人编号

记录和Audit_Details之间存在一对多关系。

我的records.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列连接两个表引起的。 将不胜感激任何建议。

问题出在我的条件而不是映射上。 通过在下面设置我的标准来解决此问题。

createAlias("auditDetails", "ad", CriteriaSpecification.LEFT_JOIN);

此外,将Hibernate更新为3.3,并移至注释和下面的设置解决了其他选择问题。

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CARRIER_NUMBER")
public Records getRecord() {
    return record;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM