views:

33

answers:

1

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 '/'
+1  A: 

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.

Pascal Thivent
Yep, your right ... it was due to a different error. Sorry for the inconvenience, but thanks for the help anyway.
stjowa