gpt4 book ai didi

hibernate - 关于 Jpa 和 Hibernate 双向一对一的 N+1 问题

转载 作者:行者123 更新时间:2023-12-01 12:43:39 24 4
gpt4 key购买 nike

正在执行的查询是:

from PurchaseOrder o join fetch o.basket where o.statusE in (:statuses)

PurchaseOrder 中的映射:

@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="basket_id")
public ShoppingBasket getBasket() {
return this.basket;
}

ShoppingBasket 中的映射:

@OneToOne(mappedBy = "basket", cascade=CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@Fetch(FetchMode.JOIN)
public PurchaseOrder getOrder() {
return order;
}

当该查询被执行时,它被翻译成以下查询:

-- 以下类型的 1 个查询,检索给定状态的所有篮子和订单

select PurchaseOrder.*, ShoppingBasket.* /* changed for simplicity */
from purchaseorders purchaseor0_
inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id
where purchaseor0_.status in (? , ? , ?)
order by purchaseor0_.created DESC limit ?

-- 以下类型的 n 个查询,一个用于检索关联订单的前一个查询的每个篮子结果

select pPurchaseOrder.* /* changed for simplicity */
from purchaseorders purchaseor0_
where purchaseor0_.basket_id=?

我尝试更改抓取策略(我知道无论如何使用 HQL 都会忽略它)、更改抓取计划以及出于绝望的“可选”属性。有什么方法可以让 Hibernate 在单个查询中加载 PurchaseOrders 和关联的 ShoppingBaskets 吗?我是否必须使用字节码检测来实现这一目标?如果是,如何实现?

Hibernate调试日志如下:

DEBUG JDBCTransaction - begin
DEBUG ConnectionManager - opening JDBC connection
DEBUG JDBCTransaction - current autocommit status: true
DEBUG JDBCTransaction - disabling autocommit
DEBUG QueryTranslatorImpl - parse() - HQL: from com.ng.ticketlogic.core.shop.PurchaseOrder o join fetch o.basket where o.statusE in (:statuses) ORDER BY o.created DESC
DEBUG AST - --- HQL AST ---
\-[QUERY] Node: 'query'
+-[SELECT_FROM] Node: 'SELECT_FROM'
| \-[FROM] Node: 'from'
| +-[RANGE] Node: 'RANGE'
| | +-[DOT] Node: '.'
| | | +-[DOT] Node: '.'
| | | | +-[DOT] Node: '.'
| | | | | +-[DOT] Node: '.'
| | | | | | +-[DOT] Node: '.'
| | | | | | | +-[IDENT] Node: 'com'
| | | | | | | \-[IDENT] Node: 'ng'
| | | | | | \-[IDENT] Node: 'ticketlogic'
| | | | | \-[IDENT] Node: 'core'
| | | | \-[IDENT] Node: 'shop'
| | | \-[IDENT] Node: 'PurchaseOrder'
| | \-[ALIAS] Node: 'o'
| \-[JOIN] Node: 'join'
| +-[FETCH] Node: 'fetch'
| \-[DOT] Node: '.'
| +-[IDENT] Node: 'o'
| \-[IDENT] Node: 'basket'
+-[WHERE] Node: 'where'
| \-[IN] Node: 'in'
| +-[DOT] Node: '.'
| | +-[IDENT] Node: 'o'
| | \-[IDENT] Node: 'statusE'
| \-[IN_LIST] Node: 'inList'
| \-[COLON] Node: ':'
| \-[IDENT] Node: 'statuses'
\-[ORDER] Node: 'ORDER'
+-[DOT] Node: '.'
| +-[IDENT] Node: 'o'
| \-[IDENT] Node: 'created'
\-[DESCENDING] Node: 'DESC'

DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG HqlSqlBaseWalker - select << begin [level=1, statement=select]
DEBUG FromElement - FromClause{level=1} : com.ng.ticketlogic.core.shop.PurchaseOrder (o) -> purchaseor0_
DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : basket -> org.hibernate.type.ManyToOneType(com.ng.ticketlogic.core.shop.ShoppingBasket)
DEBUG DotNode - dereferenceEntityJoin() : generating join for basket in com.ng.ticketlogic.core.shop.PurchaseOrder {no alias} parent = [ {null} ]
DEBUG FromElement - FromClause{level=1} : com.ng.ticketlogic.core.shop.ShoppingBasket (no alias) -> shoppingba1_
DEBUG FromClause - addJoinByPathMap() : o.basket -> FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}
DEBUG FromReferenceNode - Resolved : o.basket -> purchaseor0_.basket_id
DEBUG HqlSqlWalker - createFromJoinElement() : -- join tree --
\-[JOIN_FRAGMENT] FromElement: 'shoppingbaskets shoppingba1_' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}

DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : statusE -> org.hibernate.type.CustomType@493744b6
DEBUG FromReferenceNode - Resolved : o.statusE -> purchaseor0_.status
DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : created -> org.hibernate.type.TimestampType@69a4e343
DEBUG FromReferenceNode - Resolved : o.created -> purchaseor0_.created
DEBUG HqlSqlBaseWalker - select : finishing up [level=1, statement=select]
DEBUG HqlSqlWalker - processQuery() : ( SELECT ( FromClause{level=1} ( purchaseorders purchaseor0_ shoppingbaskets shoppingba1_ ) ) ( where ( in ( purchaseor0_.status purchaseor0_.id statusE ) ( inList ? ) ) ) ( ORDER ( purchaseor0_.created purchaseor0_.id created ) DESC ) )
DEBUG HqlSqlWalker - Derived SELECT clause created.
DEBUG JoinProcessor - Using FROM fragment [purchaseorders purchaseor0_]
DEBUG JoinProcessor - Using FROM fragment [inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id]
DEBUG HqlSqlBaseWalker - select >> end [level=1, statement=select]
DEBUG AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (purchaseorders,shoppingbaskets)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'purchaseor0_.id as id10_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=o,role=null,tableName=purchaseorders,tableAlias=purchaseor0_,origin=null,columns={,className=com.ng.ticketlogic.core.shop.PurchaseOrder}}}
| +-[SELECT_EXPR] SelectExpressionImpl: 'shoppingba1_.id as id9_1_' {FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}}
| +-[SQL_TOKEN] SqlFragment: 'purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_'
| \-[SQL_TOKEN] SqlFragment: 'shoppingba1_.call_center as call2_9_1_, shoppingba1_.call_center_fee as call3_9_1_, shoppingba1_.cardCharge as cardCharge9_1_, shoppingba1_.created as created9_1_, shoppingba1_.currency as currency9_1_, shoppingba1_.deliveryCost as delivery6_9_1_, shoppingba1_.delivery_option as delivery14_9_1_, shoppingba1_.deliveryType as delivery7_9_1_, shoppingba1_.insurance as insurance9_1_, shoppingba1_.owner as owner9_1_, shoppingba1_.promotionCode_id as promoti15_9_1_, shoppingba1_.status as status9_1_, shoppingba1_.unique_id as unique11_9_1_, shoppingba1_.updated as updated9_1_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=2, fromElements=2, fromElementByClassAlias=[o], fromElementByTableAlias=[purchaseor0_, shoppingba1_], fromElementsByPath=[o.basket], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'purchaseorders purchaseor0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=o,role=null,tableName=purchaseorders,tableAlias=purchaseor0_,origin=null,columns={,className=com.ng.ticketlogic.core.shop.PurchaseOrder}}
| \-[JOIN_FRAGMENT] FromElement: 'inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}
+-[WHERE] SqlNode: 'where'
| \-[IN] InLogicOperatorNode: 'in'
| +-[DOT] DotNode: 'purchaseor0_.status' {propertyName=statusE,dereferenceType=ALL,propertyPath=statusE,path=o.statusE,tableAlias=purchaseor0_,className=com.ng.ticketlogic.core.shop.PurchaseOrder,classAlias=o}
| | +-[ALIAS_REF] IdentNode: 'purchaseor0_.id' {alias=o, className=com.ng.ticketlogic.core.shop.PurchaseOrder, tableAlias=purchaseor0_}
| | \-[IDENT] IdentNode: 'statusE' {originalText=statusE}
| \-[IN_LIST] SqlNode: 'inList'
| \-[NAMED_PARAM] ParameterNode: '?' {name=statuses, expectedType=org.hibernate.type.CustomType@493744b6}
\-[ORDER] OrderByClause: 'ORDER'
+-[DOT] DotNode: 'purchaseor0_.created' {propertyName=created,dereferenceType=ALL,propertyPath=created,path=o.created,tableAlias=purchaseor0_,className=com.ng.ticketlogic.core.shop.PurchaseOrder,classAlias=o}
| +-[ALIAS_REF] IdentNode: 'purchaseor0_.id' {alias=o, className=com.ng.ticketlogic.core.shop.PurchaseOrder, tableAlias=purchaseor0_}
| \-[IDENT] IdentNode: 'created' {originalText=created}
\-[DESCENDING] SqlNode: 'DESC'

DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG QueryTranslatorImpl - HQL: from com.ng.ticketlogic.core.shop.PurchaseOrder o join fetch o.basket where o.statusE in (:statuses) ORDER BY o.created DESC
DEBUG QueryTranslatorImpl - SQL: select purchaseor0_.id as id10_0_, shoppingba1_.id as id9_1_, purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_, shoppingba1_.call_center as call2_9_1_, shoppingba1_.call_center_fee as call3_9_1_, shoppingba1_.cardCharge as cardCharge9_1_, shoppingba1_.created as created9_1_, shoppingba1_.currency as currency9_1_, shoppingba1_.deliveryCost as delivery6_9_1_, shoppingba1_.delivery_option as delivery14_9_1_, shoppingba1_.deliveryType as delivery7_9_1_, shoppingba1_.insurance as insurance9_1_, shoppingba1_.owner as owner9_1_, shoppingba1_.promotionCode_id as promoti15_9_1_, shoppingba1_.status as status9_1_, shoppingba1_.unique_id as unique11_9_1_, shoppingba1_.updated as updated9_1_ from purchaseorders purchaseor0_ inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id where purchaseor0_.status in (?) order by purchaseor0_.created DESC
DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG QueryTranslatorImpl - parse() - HQL: from com.ng.ticketlogic.core.shop.PurchaseOrder o join fetch o.basket where o.statusE in (:statuses0_, :statuses1_, :statuses2_) ORDER BY o.created DESC
DEBUG AST - --- HQL AST ---
\-[QUERY] Node: 'query'
+-[SELECT_FROM] Node: 'SELECT_FROM'
| \-[FROM] Node: 'from'
| +-[RANGE] Node: 'RANGE'
| | +-[DOT] Node: '.'
| | | +-[DOT] Node: '.'
| | | | +-[DOT] Node: '.'
| | | | | +-[DOT] Node: '.'
| | | | | | +-[DOT] Node: '.'
| | | | | | | +-[IDENT] Node: 'com'
| | | | | | | \-[IDENT] Node: 'ng'
| | | | | | \-[IDENT] Node: 'ticketlogic'
| | | | | \-[IDENT] Node: 'core'
| | | | \-[IDENT] Node: 'shop'
| | | \-[IDENT] Node: 'PurchaseOrder'
| | \-[ALIAS] Node: 'o'
| \-[JOIN] Node: 'join'
| +-[FETCH] Node: 'fetch'
| \-[DOT] Node: '.'
| +-[IDENT] Node: 'o'
| \-[IDENT] Node: 'basket'
+-[WHERE] Node: 'where'
| \-[IN] Node: 'in'
| +-[DOT] Node: '.'
| | +-[IDENT] Node: 'o'
| | \-[IDENT] Node: 'statusE'
| \-[IN_LIST] Node: 'inList'
| +-[COLON] Node: ':'
| | \-[IDENT] Node: 'statuses0_'
| +-[COLON] Node: ':'
| | \-[IDENT] Node: 'statuses1_'
| \-[COLON] Node: ':'
| \-[IDENT] Node: 'statuses2_'
\-[ORDER] Node: 'ORDER'
+-[DOT] Node: '.'
| +-[IDENT] Node: 'o'
| \-[IDENT] Node: 'created'
\-[DESCENDING] Node: 'DESC'

DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG HqlSqlBaseWalker - select << begin [level=1, statement=select]
DEBUG FromElement - FromClause{level=1} : com.ng.ticketlogic.core.shop.PurchaseOrder (o) -> purchaseor0_
DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : basket -> org.hibernate.type.ManyToOneType(com.ng.ticketlogic.core.shop.ShoppingBasket)
DEBUG DotNode - dereferenceEntityJoin() : generating join for basket in com.ng.ticketlogic.core.shop.PurchaseOrder {no alias} parent = [ {null} ]
DEBUG FromElement - FromClause{level=1} : com.ng.ticketlogic.core.shop.ShoppingBasket (no alias) -> shoppingba1_
DEBUG FromClause - addJoinByPathMap() : o.basket -> FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}
DEBUG FromReferenceNode - Resolved : o.basket -> purchaseor0_.basket_id
DEBUG HqlSqlWalker - createFromJoinElement() : -- join tree --
\-[JOIN_FRAGMENT] FromElement: 'shoppingbaskets shoppingba1_' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}

DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : statusE -> org.hibernate.type.CustomType@493744b6
DEBUG FromReferenceNode - Resolved : o.statusE -> purchaseor0_.status
DEBUG FromReferenceNode - Resolved : o -> purchaseor0_.id
DEBUG DotNode - getDataType() : created -> org.hibernate.type.TimestampType@69a4e343
DEBUG FromReferenceNode - Resolved : o.created -> purchaseor0_.created
DEBUG HqlSqlBaseWalker - select : finishing up [level=1, statement=select]
DEBUG HqlSqlWalker - processQuery() : ( SELECT ( FromClause{level=1} ( purchaseorders purchaseor0_ shoppingbaskets shoppingba1_ ) ) ( where ( in ( purchaseor0_.status purchaseor0_.id statusE ) ( inList ? ? ? ) ) ) ( ORDER ( purchaseor0_.created purchaseor0_.id created ) DESC ) )
DEBUG HqlSqlWalker - Derived SELECT clause created.
DEBUG JoinProcessor - Using FROM fragment [purchaseorders purchaseor0_]
DEBUG JoinProcessor - Using FROM fragment [inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id]
DEBUG HqlSqlBaseWalker - select >> end [level=1, statement=select]
DEBUG AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (purchaseorders,shoppingbaskets)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'purchaseor0_.id as id10_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=o,role=null,tableName=purchaseorders,tableAlias=purchaseor0_,origin=null,columns={,className=com.ng.ticketlogic.core.shop.PurchaseOrder}}}
| +-[SELECT_EXPR] SelectExpressionImpl: 'shoppingba1_.id as id9_1_' {FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}}
| +-[SQL_TOKEN] SqlFragment: 'purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_'
| \-[SQL_TOKEN] SqlFragment: 'shoppingba1_.call_center as call2_9_1_, shoppingba1_.call_center_fee as call3_9_1_, shoppingba1_.cardCharge as cardCharge9_1_, shoppingba1_.created as created9_1_, shoppingba1_.currency as currency9_1_, shoppingba1_.deliveryCost as delivery6_9_1_, shoppingba1_.delivery_option as delivery14_9_1_, shoppingba1_.deliveryType as delivery7_9_1_, shoppingba1_.insurance as insurance9_1_, shoppingba1_.owner as owner9_1_, shoppingba1_.promotionCode_id as promoti15_9_1_, shoppingba1_.status as status9_1_, shoppingba1_.unique_id as unique11_9_1_, shoppingba1_.updated as updated9_1_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=2, fromElements=2, fromElementByClassAlias=[o], fromElementByTableAlias=[purchaseor0_, shoppingba1_], fromElementsByPath=[o.basket], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'purchaseorders purchaseor0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=o,role=null,tableName=purchaseorders,tableAlias=purchaseor0_,origin=null,columns={,className=com.ng.ticketlogic.core.shop.PurchaseOrder}}
| \-[JOIN_FRAGMENT] FromElement: 'inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id' FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=shoppingbaskets,tableAlias=shoppingba1_,origin=purchaseorders purchaseor0_,columns={purchaseor0_.basket_id ,className=com.ng.ticketlogic.core.shop.ShoppingBasket}}
+-[WHERE] SqlNode: 'where'
| \-[IN] InLogicOperatorNode: 'in'
| +-[DOT] DotNode: 'purchaseor0_.status' {propertyName=statusE,dereferenceType=ALL,propertyPath=statusE,path=o.statusE,tableAlias=purchaseor0_,className=com.ng.ticketlogic.core.shop.PurchaseOrder,classAlias=o}
| | +-[ALIAS_REF] IdentNode: 'purchaseor0_.id' {alias=o, className=com.ng.ticketlogic.core.shop.PurchaseOrder, tableAlias=purchaseor0_}
| | \-[IDENT] IdentNode: 'statusE' {originalText=statusE}
| \-[IN_LIST] SqlNode: 'inList'
| +-[NAMED_PARAM] ParameterNode: '?' {name=statuses0_, expectedType=org.hibernate.type.CustomType@493744b6}
| +-[NAMED_PARAM] ParameterNode: '?' {name=statuses1_, expectedType=org.hibernate.type.CustomType@493744b6}
| \-[NAMED_PARAM] ParameterNode: '?' {name=statuses2_, expectedType=org.hibernate.type.CustomType@493744b6}
\-[ORDER] OrderByClause: 'ORDER'
+-[DOT] DotNode: 'purchaseor0_.created' {propertyName=created,dereferenceType=ALL,propertyPath=created,path=o.created,tableAlias=purchaseor0_,className=com.ng.ticketlogic.core.shop.PurchaseOrder,classAlias=o}
| +-[ALIAS_REF] IdentNode: 'purchaseor0_.id' {alias=o, className=com.ng.ticketlogic.core.shop.PurchaseOrder, tableAlias=purchaseor0_}
| \-[IDENT] IdentNode: 'created' {originalText=created}
\-[DESCENDING] SqlNode: 'DESC'

DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG QueryTranslatorImpl - HQL: from com.ng.ticketlogic.core.shop.PurchaseOrder o join fetch o.basket where o.statusE in (:statuses0_, :statuses1_, :statuses2_) ORDER BY o.created DESC
DEBUG QueryTranslatorImpl - SQL: select purchaseor0_.id as id10_0_, shoppingba1_.id as id9_1_, purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_, shoppingba1_.call_center as call2_9_1_, shoppingba1_.call_center_fee as call3_9_1_, shoppingba1_.cardCharge as cardCharge9_1_, shoppingba1_.created as created9_1_, shoppingba1_.currency as currency9_1_, shoppingba1_.deliveryCost as delivery6_9_1_, shoppingba1_.delivery_option as delivery14_9_1_, shoppingba1_.deliveryType as delivery7_9_1_, shoppingba1_.insurance as insurance9_1_, shoppingba1_.owner as owner9_1_, shoppingba1_.promotionCode_id as promoti15_9_1_, shoppingba1_.status as status9_1_, shoppingba1_.unique_id as unique11_9_1_, shoppingba1_.updated as updated9_1_ from purchaseorders purchaseor0_ inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id where purchaseor0_.status in (? , ? , ?) order by purchaseor0_.created DESC
DEBUG ErrorCounter - throwQueryException() : no errors
DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG SQL - select purchaseor0_.id as id10_0_, shoppingba1_.id as id9_1_, purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_, shoppingba1_.call_center as call2_9_1_, shoppingba1_.call_center_fee as call3_9_1_, shoppingba1_.cardCharge as cardCharge9_1_, shoppingba1_.created as created9_1_, shoppingba1_.currency as currency9_1_, shoppingba1_.deliveryCost as delivery6_9_1_, shoppingba1_.delivery_option as delivery14_9_1_, shoppingba1_.deliveryType as delivery7_9_1_, shoppingba1_.insurance as insurance9_1_, shoppingba1_.owner as owner9_1_, shoppingba1_.promotionCode_id as promoti15_9_1_, shoppingba1_.status as status9_1_, shoppingba1_.unique_id as unique11_9_1_, shoppingba1_.updated as updated9_1_ from purchaseorders purchaseor0_ inner join shoppingbaskets shoppingba1_ on purchaseor0_.basket_id=shoppingba1_.id where purchaseor0_.status in (? , ? , ?) order by purchaseor0_.created DESC limit ?
DEBUG AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#615430], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1608284]
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#614920], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1607774]
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#614875], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1607729]
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#610567], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1603430]
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#608160], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1601039]
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#601660], EntityKey[com.ng.ticketlogic.core.shop.ShoppingBasket#1594566]
DEBUG AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG TwoPhaseLoad - resolving associations for [com.ng.ticketlogic.core.shop.PurchaseOrder#615430]
DEBUG TwoPhaseLoad - done materializing entity [com.ng.ticketlogic.core.shop.PurchaseOrder#615430]
DEBUG TwoPhaseLoad - resolving associations for [com.ng.ticketlogic.core.shop.ShoppingBasket#1608284]
DEBUG Loader - loading entity: [com.ng.ticketlogic.core.shop.PurchaseOrder#1608284]
DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG SQL - select purchaseor0_.id as id10_0_, purchaseor0_.address_id as address17_10_0_, purchaseor0_.affiliate_id as affiliate18_10_0_, purchaseor0_.agreed_cardholder as agreed2_10_0_, purchaseor0_.agreed_terms as agreed3_10_0_, purchaseor0_.alternativeCustomerName as alternat4_10_0_, purchaseor0_.approved as approved10_0_, purchaseor0_.author as author10_0_, purchaseor0_.basket_id as basket19_10_0_, purchaseor0_.card_id as card20_10_0_, purchaseor0_.created as created10_0_, purchaseor0_.editor as editor10_0_, purchaseor0_.groupBookingId as groupBo21_10_0_, purchaseor0_.ip_address as ip9_10_0_, purchaseor0_.linkedOrder as linkedO22_10_0_, purchaseor0_.manual_hold as manual10_10_0_, purchaseor0_.manual_review as manual11_10_0_, purchaseor0_.partner_id as partner23_10_0_, purchaseor0_.platformType as platfor12_10_0_, purchaseor0_.reference as reference10_0_, purchaseor0_.status as status10_0_, purchaseor0_.updated as updated10_0_, purchaseor0_.user_id as user24_10_0_, purchaseor0_.views as views10_0_ from purchaseorders purchaseor0_ where purchaseor0_.basket_id=?
DEBUG AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG Loader - result row: EntityKey[com.ng.ticketlogic.core.shop.PurchaseOrder#615430]
DEBUG AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG Loader - done entity load
DEBUG TwoPhaseLoad - done materializing entity [com.ng.ticketlogic.core.shop.ShoppingBasket#1608284]

最佳答案

Using FetchMode.JOIN disables LAZY loading.产生的行为可能不是您所期望的。试试这个:

@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="basket_id")
public ShoppingBasket getBasket() {
return this.basket;
}

@OneToOne(mappedBy = "basket", cascade=CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
public PurchaseOrder getOrder() {
return order;
}

之后,您的查询将只执行单个数据库查询。结果将包含一个 PurchaseOrder 对象列表,每个对象都具有正确初始化的购物篮属性。

附带说明:双向、相互需要、一对一的关系真的是必需的吗?从业务的角度来看,采购订单是在购物篮的生命周期之后创建的,因此不需要对它的引用(它应该包含基于购物篮内容创建的订单项目列表)。但这只是一个建议。

关于hibernate - 关于 Jpa 和 Hibernate 双向一对一的 N+1 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19322668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com