(n+1) select problem which I am not able to resolve. I am joining tables with non-PK column.
My schema
Records
Record_Id (PK)
Carrier_Number
Audit_Details
AuditId(PK)
Carrier_Number
There is a one-to-many relationship between Records and Audit_Details.
My 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>
My 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"/>
This produces a query like
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=?
I have tried changing to fetch="select" , changing lazy="false" and lazy="no-proxy" but nothing has worked so far. I am not sure if this problem is because of joining two tables with nonPK column. Would appreciate any suggestions.
The problem was with my Criteria and not mapping. Solved this problem by setting below for my criteria.
createAlias("auditDetails", "ad", CriteriaSpecification.LEFT_JOIN);
Also, updated Hibernate to 3.3 and moved to annotations and setting below solved additional selects problem.
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CARRIER_NUMBER")
public Records getRecord() {
return record;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.