2017-04-06 46 views
0

想象我有2个实体和查询:Hibernate的HQL构造表达

select p, n.name 
    from ProductOffering p 
    left join ProductOfferingName n on (n.productOfferingId =  p.productOfferingId and n.salesChannelId= :salesChannelId) 

Hibernate会发出SQL查询与p和名称列从n个所有colums并返回我的对象​​[]。 但处理的对象[]是不是类型安全的,所以我会尝试使用“构造表达”:

select new com.peterservice.ccm.pom.internal.api.model.ProductOfferingResult(p, n.name) 
    from ProductOffering p 
    left join ProductOfferingName n on (n.productOfferingId = p.productOfferingId and n.salesChannelId= :salesChannelId) 

随着该查询休眠只选择p.id + n.name并为每一行它问题“select * from ProductOffering p其中p.id =:id”(n + 1问题)

该行为是否正常并且是预期的?

回答

0

尝试改变 “左连接抓取”:

select new com.peterservice.ccm.pom.internal.api.model.ProductOfferingResult(p, n.name) 
    from ProductOffering p 
    left join fetch ProductOfferingName n on (n.productOfferingId = p.productOfferingId and n.salesChannelId= :salesChannelId)