JPQL:如何在查询字符串中包含正斜杠?

发布于 2024-09-27 16:08:45 字数 442 浏览 1 评论 0原文

如何正确转义 JPQL 查询字符串中的“/”正斜杠?

如果我这样做:

LOCATE('/', REVERSE( ...

我得到:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

但是,如果我这样做:

LOCATE('\\', REVERSE( ...

一切都很好。

那么,如何包含正斜杠?

编辑:我已经尝试过以下方法,但它们不起作用:

'\\/'
'//'
CHAR(47)
ESCAPE '/'

How do you properly escape a '/' forward slash in a JPQL query string?

If I do this:

LOCATE('/', REVERSE( ...

I get:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

However, if I do this:

LOCATE('\\', REVERSE( ...

Everything is fine.

So, how do I include the forward slash?

EDIT: I've already tried the following and they don't work:

'\\/'
'//'
CHAR(47)
ESCAPE '/'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2024-10-04 16:08:45

无法使用所提供的详细信息进行重现,以下内容对我有用:

@Test
public void testQueryWithLocateKeyword() {
    Product p1 = new Product("1234/Foo");
    Product p2 = new Product("12/Bar");
    em.persist(p1);
    em.persist(p2);
    em.flush();

    String qlString = "SELECT LOCATE('/', p.name) FROM Product p";

    List actual = em.createQuery(qlString).getResultList();

    List<Integer> expected = Arrays.asList(5, 3);

    assertNotNull(actual);
    ReflectionAssert.assertReflectionEquals(expected, actual);
}

使用 Hibernate EM 3.5(和 H2)进行测试。

请提供代表性查询、完整的堆栈跟踪、Hibernate 版本。

Can't reproduce with the provided details, the following works for me:

@Test
public void testQueryWithLocateKeyword() {
    Product p1 = new Product("1234/Foo");
    Product p2 = new Product("12/Bar");
    em.persist(p1);
    em.persist(p2);
    em.flush();

    String qlString = "SELECT LOCATE('/', p.name) FROM Product p";

    List actual = em.createQuery(qlString).getResultList();

    List<Integer> expected = Arrays.asList(5, 3);

    assertNotNull(actual);
    ReflectionAssert.assertReflectionEquals(expected, actual);
}

Tested with Hibernate EM 3.5 (and H2).

Please provide a representative query, a full stacktrace, the Hibernate version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文