我得到了一個作業專案,包括 JPA / Hibernate
例如,我有一個物件,可以說描述為
class SampleObject {
string value;
string othervalue;
}
假設資料庫中的值填充了 2 行
表 - SampleObject
第 1 行 - value = "21", othervalue = "some other value"
第 2 行 - value = "31", othervalue = "其他一些值"
我可以撰寫一個回傳以下行的查詢
Query q = entityManager.createQuery("select s from SampleObject s where value = 21")
我可以撰寫一個回傳以下行的查詢
Query q = entityManager.createNativeQuery("select * from dbo.SampleObject s where value = 21", SampleObject.class)
但我不能做的是將這些相同的查詢寫為字串值并回傳資料
例 1:
entityManager.createQuery("select s from SampleObject s where value = '21'")
例 2:
entityManager.createQuery("select s from SampleObject s where value = :val").setParameter("val", "21")
例 3:
entityManager.createNativeQuery("select * from dbo.SampleObject s where value = '21'", SampleObject.class)
但正如您所看到的,它存盤為 varchar 并且需要保留為字串,因此我需要能夠通過字串進行查詢
免責宣告:
我是休眠新手-非常新....非常非常新!
我應該添加 SQL SSMS 我可以直接按字串查詢
我還嘗試使用以下建議之一,但對于背景關系,它沒有解決

uj5u.com熱心網友回復:
你可以使用:
entityManager.unwrap(Session.class)
.createQuery("<your query>")
.setParameter("identifier", "3", StringType.INSTANCE);
該實作適用于您要傳遞的確切型別。
uj5u.com熱心網友回復:
我必須道歉我有一個舊的連接字串
我直接在 ssms 中查詢當前資料,而連接字串正在抓取 java 中的舊資料
我的錯,它一直在作業
通過除錯器顯示舊資料
謝謝您的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406478.html
標籤:
