簡體   English   中英

在SQL中用作別名時將不存在列存儲在JPA實體中

[英]Store non existence column in JPA entity when it is used as Alias in SQL

我已編寫查詢來計算指定企業下可用位置的數量。 SQL查詢是這樣的

SELECT 
e.enterprise_id,
e.ent_name,
(
    SELECT count(l.location_id)
    FROM rfx_ent_locations l
    WHERE l.loc_type_id <> 1001
        AND e.enterprise_id = l.enterprise_id
) AS locCount
FROM rfx_enterprises e
WHERE  e.partner_type = 102;

如果將其作為實體中的列進行介紹,則會收到一條錯誤消息,指出未找到任何列。 要檢索包含用作別名的locCount的實體,應使用JPA如何進行?

不要用那做計數查詢。像這樣單獨做

Query query = session.createQuery("SELECT count(l.location_id)
    FROM rfx_ent_locations l
    WHERE l.loc_type_id <> 1001
        AND e.enterprise_id = l.enterprise_id");// do as you please with the query result

    /**
     * instead of doing a second query to get the count.  Just do a quick count using Iterator
     */

    for (Iterator iterator = query.iterate(); iterator.hasNext();) {
        iterator.next();
        count++;
    }

    Integer queryCount = count;

將此queryCount設置為類中的@transient變量setter方法,這樣您就可以進行計數了

要么
這樣嘗試

Integer count = (Integer) session.CreateQuery("SELECT count(l.location_id)
FROM rfx_ent_locations l
WHERE l.loc_type_id <> 1001
    AND e.enterprise_id = l.enterprise_id").UniqueResult();

干杯!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM