![](/img/trans.png)
[英]Using Hibernate Criteria API To Query Many-To-One Relationships with Projections
[英]Implementing SQL query as JPA Criteria for two Many-To-One relationships
我正在嘗試為以下場景構建 JPA 標准。 我有以下 3 個 Entity 類,其中兩個與 UniqueType 有@ManyToOne 關系,但 UniqueType 與任何其他 class 沒有顯式關系:結構如下:
------------- ----------- ------------
event N:1 unique_type 1:N detail
------------- ----------- ------------
event_id (PK) type_code (PK) detail_id (PK)
type_code (FK) type_code (FK)
abbreviation
成功選擇通過縮寫過濾的事件記錄的 SQL 查詢:
SELECT
event.event_id,
event.type_code,
detail.abbreviation
FROM
event
JOIN unique_type ON event.type_code = unique_type.type_code
JOIN detail ON detail.type_code = unique_type.type_code
WHERE detail.abbreviation = 'ABC'
我有事件 class 作為我的根,但是當我加入 UniqueType class 時,我被卡住了,因為與 UniqueType 上的詳細信息沒有明確的關系
Join<Event, UniqueType> joinToUniqueType = root.join(Event_.typeCode);
Join<UniqueType, ???>
我希望能夠搜索類似的縮寫:
predicates.add(builder.lower(joinToDetail.get(Detail_.abbreviation)),'ABC');
我怎樣才能寫出標准? 任何幫助深表感謝。
由於您需要內部聯接語義,因此您可以使用交叉聯接來代替,數據庫無論如何都會對其進行優化。 使用以下
Root<Detail> event = query.from(Event.class);
Root<Detail> detail = query.from(Detail.class);
predicates.add(builder.eq(detail.get(Detail_.typeCode), event.get(Event_.typeCode));
predicates.add(builder.lower(detail.get(Detail_.abbreviation)),'ABC');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.