jpa mysql 函数,在JPA查询中使用MySQL的LPAD字符串函数?

May I know how to convert the following MySQL SQL to JPA query?

SELECT * FROM ORDER

WHERE LPAD(BATCH, 2, ' ') < LPAD('X', 2, ' ') ;

What I couldn't figure out is the LPAD() string function in JPA.

Do you have any idea?

解决方案

I know that is a very old post, but i am always be questioned by some colleagues when they are building JPA queries how to do the same thing.

Almost never you will find a perfect fit function to do what do you want. But, using some logic you can get the same result, in full JPA.

A example of LPAD.

Expression numberWithZeroLeft = builder.concat("0000", root.get("myNumber")); //Return something like 000012

Expression expressionLength = builder.length(numberWithZeroLeft);

Expression numberLpad = builder.substring(numberWithZeroLeft, e.builder.sum(expressionLength, -3), e.builder.literal(4)); //Return something like 0012

query.select(numberLpad);

Think in something like:

I have a string X, and need a string formatted in 4 zeros.

So we will get 0000 + X => "0000X"

And now just get the 4 last characters using substring.

I hope that help someone.

Cheers. =]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值