选择实体的另一个属性的属性

68bkxrlz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(300)

目前我有三个实体 Country , Office 用户,在哪里 Country.gov 属于类型 Office 在哪里 Office.holder 属于类型 User . 国家是世界的主人 Country.gov 办公室是公司的主人 Office.holder .
现在我想得到 Country.gov 用一个 LEFT JOINOffice.holder 使用国家属性,例如。 SELECT c.gov o FROM Country c LEFT JOIN FETCH c.gov LEFT JOIN FETCH c.gov.holder WHERE c.countryKey = :countryKey ,但这不起作用,它引发了一个异常:

org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list 
[FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=dev.teamnight.game.entities.Country.gov,tableName=office,tableAlias=office1_,origin=country country0_,columns={country0_.gov_id,className=dev.teamnight.game.entities.Office}}] 
[SELECT c.gov FROM dev.teamnight.game.entities.Country c LEFT JOIN FETCH c.gov LEFT JOIN FETCH c.gov.holder WHERE c.countryKey = :countryKey]
lbsnaicq

lbsnaicq1#

您可以简化一点查询:

List<Office> calls = entityManager.createQuery(
    "select o " +
    "from Country c " +
    "join c.gov o " +
    "left join fetch o.holder " +
    "where c.countryKey = :countryKey ", Office.class )
.setParameter( "countryKey", countryKey )
.getResultList();

有关更多说明,请参阅hibernate文档的这一部分。

相关问题