當我在欄位上為如下物體使用動態映射時
@Entity
class Company{
//other fields removed
@Formula("select count(c.id) from Contact c where c.company_id = id")
private int contactCount;
}
從 db 獲取公司時出現以下錯誤。
您的 SQL 語法有錯誤;檢查與您的 MySQL 服務器版本相對應的手冊以獲取正確的語法使用
但是當我將公式更改為
@Formula("(select count(c.id) from Contact c where c.company_id = id)")
private int contactCount;
一切正常。公式末尾和開頭的括號有什么作用?
uj5u.com熱心網友回復:
據我所知,在@Formula 中,JPA 使用子查詢,因此通常您使用欄位值撰寫公式并執行“數學”,例如 @Formula("field A * field B * 100"); 由于您使用的是另一個查詢,因此需要將 () 包裝起來。希望我能幫上忙。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/498179.html
