-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate/JPA CriteriaQuery trim function wrong SQL Postgres
PostPosted: Thu Jan 12, 2017 7:26 am 
Newbie

Joined: Thu Jan 12, 2017 7:09 am
Posts: 3
Location: South Africa
Does Hibernate/JPA CriteriaQuery trim function generate the wrong SQL for PostgreSQL?

Please note that this is a cross posting from this SO question

I'm trying to use the Criteria Query trim() function to strip the leading zero's of a field in the database. The relevant part of the criteria query is:
Code:
final Expression<String> _code = cmm.get(CodeMasterMapping_.code);
builder.equal(builder.trim(Trimspec.LEADING, Character.valueOf('0'), _code), productCode)));

The SQL generated by this snippet is:
Code:
trim(LEADING 0 from codemaster3_.comm_code)=?)

This leads to the following exception from Postgres:
Quote:
PSQLException: ERROR: function pg_catalog.ltrim(character varying, integer) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.

I then changed the zero '0' to a character, thinking that maybe postgres is interpreting the zero '0' as an integer, hence triggering the exception.

Changing the character to be removed to an 'A':
Code:
builder.equal(builder.trim(Trimspec.LEADING, Character.valueOf('A'), _code), productCode)));

but that generates this SQL:
Code:
trim(LEADING A from codemaster3_.comm_code)=?)

and the following SQL exception:
Quote:
PSQLException: ERROR: column "a" does not exist

According to the Postgres docs for 9.5 the trim function take these parameters:
Code:
trim([leading | trailing | both] [characters] from string)

and the given example is:
Code:
trim(both 'x' from 'xTomxx') --> 'Tom'

I have tested this on the following combinations with the exact same results
    Postgres 9.5.5, Wildfly 9.0.2.Final with Hibernate Core 4.3.10.Final
    Postgres 9.5.5, Wildfly 10.1.0.Final with Hibernate Core 5.0.10.Final
Please let me know if you need more code for the Criteria Query.

Is there something wrong with my use of the CriteriaBuilder.trim() function or should Hibernate generate SQL that use single quotes for the trim character?


Top
 Profile  
 
 Post subject: Re: Hibernate/JPA CriteriaQuery trim function wrong SQL Postgres
PostPosted: Thu Jan 12, 2017 7:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Andrea tried it and said it replicates. Can you open a JIRA issue for this?


Top
 Profile  
 
 Post subject: Re: Hibernate/JPA CriteriaQuery trim function wrong SQL Postgres
PostPosted: Fri Jan 13, 2017 6:12 am 
Newbie

Joined: Thu Jan 12, 2017 7:09 am
Posts: 3
Location: South Africa
I can also confirm that it is a bug in Hibernate ORM 5.2.6.

Issue - HHH-11393


Top
 Profile  
 
 Post subject: Re: Hibernate/JPA CriteriaQuery trim function wrong SQL Postgres
PostPosted: Thu Jan 19, 2017 5:19 pm 
Newbie

Joined: Thu Jan 12, 2017 7:09 am
Posts: 3
Location: South Africa
SOLVED with Hibernate ORM 5.2.7.Final

Thank you guys for the quick fix! You rock @vlad @andrea


Top
 Profile  
 
 Post subject: Re: Hibernate/JPA CriteriaQuery trim function wrong SQL Postgres
PostPosted: Fri Jan 20, 2017 2:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You're welcome.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.