spring-data-jpa 尝试使用自定义查询调用JPA函数时出现语法错误

zf9nrax1  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(191)

在我的存储库中,我有以下内容

public interface TenantRepository extends JpaRepository<Tenant, UUID> {
    ...

    @Query("select substring(email from '@(.*)$') as domain from Tenant group by domain")
    public List<String> findAllDomain();
}

当我试图执行它时,我得到一个错误,它说:

Syntax error in SQL statement "select substring(tenant0_.email, [*]from, '@(.*)ParseError: KaTeX parse error: Can't use function '\)' in math mode at position 2: '\̲)̲&nbsp;as&nbsp;c…') as col_0_0_ from tenants tenant0_ group by col_0_0_

因此,我将查询更改为:

"select substring(email from '@(.*)\\$') as domain from Tenant group by domain"

但它得到了另一个错误:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "select substring(tenant0_.email, [*]from, '@(.*)\\$') as col_0_0_ from tenants tenant0_ group by col_0_0_"; expected "INTERSECTS (, NOT, EXISTS, UNIQUE, INTERSECTS"; SQL statement:
select substring(tenant0_.email, from, '@(.*)\$') as col_0_0_ from tenants tenant0_ group by col_0_0_

当我在我的PgAdmin4上尝试查询时,它工作了。
有什么建议吗?
谢谢。

yxyvkwin

yxyvkwin1#

我假设您正在尝试获取所有的电子邮件域。

@Query("select substring(t.email, locate('@', t.email) + 1) as domain from Tenant t group by domain")
    public List<String> findAllDomain();

数据库中的电子邮件

aaa@domain1.com
bbbb@domain2.com
ccccccc@domain2.com

查询响应

domain1.com, domain2.com

相关问题