【发布时间】:2022-01-07 00:12:29
【问题描述】:
我在工作中得到了一个项目,包括 JPA / Hibernate
例如,我有一个对象,可以说是
class SampleObject {
string value;
string othervalue;
}
假设数据库中的值填充了 2 行
表格 - 样本对象
-
第 1 行 - value = "21", othervalue = "some other value"
-
第 2 行 - value = "31", othervalue = "some other other value"
我可以编写一个返回以下行的查询
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 并且需要保留为字符串,因此我需要能够通过字符串查询
免责声明:
我是 hibernate 新手 - 非常新......非常非常新!
我应该添加SQL SSMS 我可以直接通过字符串查询
【问题讨论】: