1

当@OneToMany 的目标是映射到多个表的实体(使用@SecondaryTable)并且排序属性位于辅助表中时,我在尝试使用@OrderBy 注释对@OneToMany 集合进行排序时遇到问题。

尝试访问属性时会引发“未找到列”异常,因为生成的 SQL order by 子句使用主表而不是辅助表的别名引用排序列。

我看不到我可以添加任何东西来完成这项工作。有什么想法吗?

public class Product{

    @OneToMany(orphanRemoval = true, mappedBy = "product", cascade = CascadeType.ALL)
    // @OrderBy(value = "displaySequence")
    public Set<Benefit> getBenefits() {
    return benefits;
    }
}

public class Benefit{
    @Column(table = SECONDARY_TABLE_NAME, name = "display_sequence", nullable = false)
    public Integer getDisplaySequence() {
    return displaySequence;
    }
}

这是使用 JPA @OrderBy 注释而不是特定于 Hibernate。

Hibernate 的版本是 3.6.9。

在这些类中,所有内容都使用属性进行映射。

其他不相关的类使用混合访问模式

有一个现有的 JIRA 项目似乎证明了与 @Where 子句相同的问题:

https://hibernate.onjira.com/browse/HHH-4246

例外是标准 SQL 无效列错误。

生成的 SQL 如下。'order by' 应参考 display_sequence 并参考别名 benefit0_。

select 
.....
from as_benefits_additional_data benefits0_ 
inner join as_benefits benefit1_ on benefits0_.id=benefit1_.id 
left outer join as_benefits_additional_data benefit1_1_ on benefit1_.id=benefit1_1_.id 
inner join as_products product2_ on benefit1_1_.product_id=product2_.id 
left outer join as_products_additional_data product2_1_ on product2_.id=product2_1_.id 
where benefits0_.product_id=1 
order by benefit1_.display_sequence asc
4

0 回答 0