在hql中左连接第一个匹配

时间:2016-02-10 16:01:24

标签: java oracle hibernate hql

select 
    schedulingpool.id,
    schedulingpool.allocationVersion,
    schedulingpool.tx.statusId,
    schedulingpool.name,
    schedulingpool.description,
    schedulingpool.schedulingPoolAddresses.name,
    schedulingpool.schedulingPoolAddresses.zip,
    schedulingpool.schedulingPoolAddresses.city,
    schedulingpool.schedulingPoolAddresses.countryId,
    schedulingpool.schedulingPoolAddresses.street,
    schedulingpool.statusId,
    schedulingpool.tx.name,
    schedulingpool.note1,
    schedulingpool.note2
from 
    com.xx.module.mm.jpa.SchedulingPool as schedulingpool 
left join schedulingpool.tx
left join schedulingpool.schedulingPoolAddresses
where schedulingpool.clientId = :clientId
     order by schedulingpool.id DESC
  

javax.servlet.ServletException:java.lang.IllegalArgumentException:   org.hibernate.QueryException:非法尝试取消引用   集合[scheduling0_.id.schedulingPoolAddresses]与元素   属性引用[name]

调度池可以有多个地址,当我执行语句时,我得到了异常。 有没有办法只获取匹配的第一个地址并加入它以防止此异常?

1 个答案:

答案 0 :(得分:0)

您需要使用别名

select 
    schedulingpool.id,
    schedulingpool.allocationVersion,
    tx.statusId,
    schedulingpool.name,
    schedulingpool.description,
    schedulingPoolAddresses.name,
    schedulingPoolAddresses.zip,
    schedulingPoolAddresses.city,
    schedulingPoolAddresses.countryId,
    schedulingPoolAddresses.street,
    schedulingpool.statusId,
    tx.name,
    schedulingpool.note1,
    schedulingpool.note2
from 
    com.xx.module.mm.jpa.SchedulingPool as schedulingpool 
left join schedulingpool.tx tx
left join schedulingpool.schedulingPoolAddresses schedulingPoolAddresses
where schedulingpool.clientId = :clientId
     order by schedulingpool.id DESC

您需要在SchedulingPool课程中与schedulingPoolAddressestx建立关联。