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

问题描述

目前我有三个实体 Login.shCountry 和 User,其中 Office 的类型为 Country.govOffice 的类型为 {{1} }. Country 是 Office.holder 的拥有方,Office 是 User 的拥有方。

现在我想使用 Country 的属性Country.gov 上获得带有 Office.holderCountry.gov,例如 LEFT JOIN,但这不起作用,它会抛出异常:

Office.holder

解决方法

您可以稍微简化一下您的查询:

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 文档的 this section