jpa mysql索引,使用数据库功能的JPA查询以使用特定索引

I have a table named vehicle which has a column named VIN.

Users need the ability to search based on the last 6 characters of the VIN.

So I've added an index in Postgres like so:

CREATE INDEX "ix_vin_last_6" ON vehicle(RIGHT(vin,6));

I added a few test records as follows:

LAST6VIN_A_123456

LAST6VIN_B_123456

LAST6VIN_C_123456

Using SpringDataJPA repository, here is one way to query for the above test records:

List findByVinEndingWith(String vin)

But the JPA query generated above uses a LIKE clause, like so:

where

vehicle0_.vin like ?

How do I make a repository method to query for the above 3 test records using the last 6 characters that uses the index I created above so that it generates this query:

SELECT vin, year, make, model

FROM vehicle

WHERE RIGHT(vin,6) = '123456';

The above query works fine when I execute it using pgadmin command line.

解决方案

JPQL supports (since JPA 2.1 IIRC) calling arbitrary database functions using function:

select v from Vehicle v

where function('RIGHT', v.vin, 6) = :endOfVin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值