Uploaded image for project: 'Hibernate ORM'
  1. HHH-11778

Inconsistent handling of CriteriaBuilder.literal() depending on context


    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects versions: 5.2.10
    • Fix versions: 6.0.0.Beta1
    • Components: hibernate-core
    • Labels:
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
    • Sprint:


      I have the following code snippet:

      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<String> q = cb.createQuery(String.class);
      Root<JPABook> from = q.from(JPABook.class);
                 .when(cb.equal(from.get("title"), cb.literal("Animal Farm")), cb.literal("Animal Farm"))

      It generates this SQL string:

      select case when jpabook0_.title=? then 'Animal Farm' else cast(null as varchar(255)) end as col_0_0_ from t_book jpabook0_

      As you can see, the literal in the CASE expression's predicate is sent to the server as a bind variable, whereas the literal in the THEN clause is sent as a literal.

      I personally believe that the latter is correct because the intention of using CriteriaBuilder.literal() is for an actual literal to make it into the generated SQL string. Because if users wanted a parameter, they could have used CriteriaBuilder.parameter() instead. What's the point of literal() if it still generates a parameter.

      I've already had this discussion with Vlad Mihalcea on Twitter:

      Even if you might not agree with the above, I think the behaviour of all CriteriaBuilder.literal() calls should be consistent, so:

      • Either generate only actual literals in generated SQL (properly escaping them of course!)
      • Or generate only bind variables in generated SQL

      Side-note: try using a Java string containing an apostrophe instead in both places: "This isn't working" and see what happens...

      Related issue: https://hibernate.atlassian.net/browse/HHH-9576




            • Votes:
              0 Vote for this issue
              3 Start watching this issue


              • Created: