JPA CriteriaBuilder就像是双重 [英] JPA CriteriaBuilder like on double

查看:81
本文介绍了JPA CriteriaBuilder就像是双重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从旧版数据库中检索数据.

I'm trying to retrieve data out of a legacy database.

表中的列定义为包含帐号的DECIMAL(13,0).

The column in the table is defined as a DECIMAL(13,0) containing account numbers.

不能更改列数据类型,因为它将对旧系统产生重大影响.基本上,所有使用该表的程序都需要更改,然后重新编译,这不是一个选择.

The column data type cannot be changed as it will have a major impact on the legacy system. Essentially all programs using the table need to be changed and then recompiled which is not an option.

我们有一项要求,即找到该帐号包含一个值的所有记录,例如,用户可以搜索12345,并且应返回所有包含12345的帐号.

We have a requirement to find all records where the account number contains a value, for example the user could search for 12345 and all accounts with an account number containing 12345 should be returned.

如果这是一个CHAR/VARCHAR,我将使用:

If this was a CHAR/VARCHAR, I would use:

criteriaBuilder.like(root.<String>get(Record_.accountNumber), searchTerm)

由于将列定义为DECIMAL(13,0),accountNumber属性为双精度.

As a result of the column defined as DECIMAL(13,0), the accountNumber property is a double.

有没有办法对DECIMAL/double字段执行赞?

Is there a way to perform a like on a DECIMAL/double field?

SQL将是

SELECT * FROM ACCOUNTS WHERE accountNumber LIKE '%12345%'

推荐答案

我实际上没有尝试过,但是我认为它应该可以工作

I have not actually tried this, but I believe it should work

criteriaBuilder.like(
    root.get(Record_.accountNumber).as(String.class),
    searchTerm)

这应生成如下查询类型:

This should generate a query kind of like this:

SELECT * FROM ACCOUNTS WHERE CAST(accountNumber AS text) LIKE '%12345%'

这篇关于JPA CriteriaBuilder就像是双重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆