Select 实体另一个属性的属性
Select attribute of another attribute of entity
目前我有三个实体 Country
、Office
和 User,其中 Country.gov
是 Office
类型,Office.holder
是 User
。 Country 是 Country.gov
的拥有方,Office 是 Office.holder
.
的拥有方
现在我想使用 Office.holder
上的 LEFT JOIN
使用国家/地区的属性来获取 Country.gov
,例如
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]
您可以稍微简化一下查询:
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();
有关其他说明,请参阅 this section 休眠文档。
目前我有三个实体 Country
、Office
和 User,其中 Country.gov
是 Office
类型,Office.holder
是 User
。 Country 是 Country.gov
的拥有方,Office 是 Office.holder
.
现在我想使用 Office.holder
上的 LEFT JOIN
使用国家/地区的属性来获取 Country.gov
,例如
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]
您可以稍微简化一下查询:
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();
有关其他说明,请参阅 this section 休眠文档。