Dmitriy Sulimchuk
Dmitriy Sulimchuk

Reputation: 1

Hibernate HQL constructor expression

Imagine I have 2 entities and that query:

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

Hibernate will issue sql query with all colums from p and name column from n and return me Object[]. But dealing with Object[] is not type-safe, so I'll try to use "constructor expression":

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)

With that query Hibernate only select p.id + n.name and for each row it issue "select * from ProductOffering p where p.id = :id" (n + 1 problem)

Is that behaviour normal and expected?

Upvotes: 0

Views: 409

Answers (1)

Alex
Alex

Reputation: 11579

Try to change to "left join fetch":

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)

Upvotes: 0

Related Questions