简体   繁体   English

jpql比较字符串为int

[英]jpql compare string as int

I have object called info 我有一个叫做info对象

class info
@Column(name="id")
int id
@Column(name="system")
string system
@Column(name="triesErrCount")
string triesErrCount

and same type in DB. 和数据库中的相同类型。 Now triesErrCount is String (in the class and in DB (varchar)), but in my db it's all like numbers ("1","7","56"...). 现在triesErrCountString (在类和DB(varchar)中),但是在我的db中,它们都像数字(“ 1”,“ 7”,“ 56” ...)。

I try to get all infos that are less then 50, I try this: 我尝试获取小于50的所有信息,请尝试以下操作:

SELECT x from PaymentInfo x where x.triesErr <=:triesErrAsString

triesErrAsString = "50" in jpql tryErrAsString =“ 50”(在jpql中)

But it's not working, I also try: 但这不起作用,我也尝试:

class info
@Column(name="id")
int id
@Column(name="system")
string system
@Transient
string triesErrCount
@Column(name="triesErrCount")
int triesErrCountAsInt

SELECT x from PaymentInfo x where x.triesErr <=:triesErrAsInt

triesErrAsInt = 50 tryErrAsInt = 50

But still not working. 但仍然无法正常工作。 I'm using jpa 1.0. 我正在使用jpa 1.0。 and OpenJPA 1.2.1 和OpenJPA 1.2.1

A string is not an Int, so numeric comparisons will not work as you are expecting. 字符串不是Int型,因此数字比较将无法正常工作。 You need to convert the string to a numeric to be able to use the numeric operators. 您需要将字符串转换为数字才能使用数字运算符。 See CAST for an example of how you might do that in JPQL, but you might be better off changing the type in the database to a numeric type. 有关如何在JPQL中执行此操作的示例,请参见CAST ,但是最好将数据库中的类型更改为数字类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM