Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:55:41+00:00 2026-06-09T20:55:41+00:00

I’m having difficulty using the CITEXT datatype in PostgreSQL using JPA and Hibernate. CITEXT

  • 0

I’m having difficulty using the CITEXT datatype in PostgreSQL using JPA and Hibernate. CITEXT is supposed to provide a case insensitive text datatype but when used with JPA/Hibernate it doesn’t behave in a case insensitive manner. Has anyone else had this issue or know a way around it? I have seen some mention (but very, very little) about a JDBC issue, but that went back at least a year and wasn’t very clear.

I have a ‘nickname’ column defined as citext in postgres 9.1. I just did a test to see if it could find a row using a named query as such:

create table test(
    nickname citext
)

@NamedQuery(name = "Person.findByNickname", 
            query = "SELECT p 
                     FROM Person p 
                     WHERE p.nickname = :nickname")

Insert a nickname into the DB:

insert into test values('testNick')

Then run this code:

String nickname = "testNick";

Query q = em.createNamedQuery("Person.findByNickname");
q.setParameter("nickname", nickname);
if (q.getResultList().isEmpty()) {
    return (false);
}
return (true);

This returns ‘true’ (i.e. there is already a ‘testNick’ in the database).

If I make this assignment

String nickname = "testnick"; //(lower case 'N') 

and run it again it returns ‘false’.

Since the column is CITEXT, it should return ‘true’ again. i.e. case insensitive text.

Using JPA and Hibernate. Anyone have any thoughts?

In the meantime I’ve changed the column back to varchar and created a functional index for lowercase. And I have to create a native query now to search using database functions. Would like to find out if there is a way I can not have to do this to maintain the database abstraction.

Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:55:43+00:00Added an answer on June 9, 2026 at 8:55 pm

    citext provides case-insensitive operators for use within the database, with other citext values.

    What’s happening

    At a guess, your JPA implementation is explicitly specifying the type of the parameter as text when it creates the parameterized statement. citext doesn’t define a citext = text operator, so PostgreSQL casts the citext to text and uses the case-sensitive text = text operator. Effectively, comparing citext to text is case sensitive.

    Here’s what I think is happening. Given the dummy data:

    regress=# CREATE EXTENSION citext;
    regress=# CREATE TABLE citest ( x citext );
    regress=# INSERT INTO citest(x) VALUES ('FRED'), ('FrEd');
    regress=# SELECT * FROM citest;
      x   
    ------
     FRED
     FrEd
    (2 rows)
    

    … a comparison of citext to an unknown string literal will be interpreted as citext=citext and be done case-insensitively:

    regress=# SELECT * FROM citest WHERE x = 'FRED';
      x   
    ------
     FRED
     FrEd
    (2 rows)
    

    … but a comparison between citext and an explicitly text typed literal will convert the citext argument to text using citext‘s implicit cast to text, then do a text=text case sensitive comparison:

    regress=# SELECT * FROM citest WHERE x = 'FRED'::text;
      x   
    ------
     FRED
    (1 row)
    

    Or rather, what Hibernate is doing will be closer to:

    regress=# PREPARE blah(text) AS SELECT * FROM citest WHERE x = $1;
    PREPARE
    regress=# EXECUTE blah('FRED');
      x   
    ------
     FRED
    (1 row)
    

    where the type is specified as text when binding a parameter, since Hibernate “knows” that Strings are text.

    In other words, you need to get Hibernate to, via PgJDBC, explicitly specify the citext datatype as the parameter type to your query, resulting in something like:

    regress=# PREPARE blah(citext) AS SELECT * FROM citest WHERE x = $1;
    PREPARE
    regress=# EXECUTE blah('FRED');
      x   
    ------
     FRED
     FrEd
    (2 rows)
    

    Note the explicit citext type parameter to the prepared statement. That will be … interesting … to do, especially since PgJDBC doesn’t know anything about the citext type. You’d have to write a custom data type handler for Hibernate that uses PgJDBC’s setObject; even then you’ll have operator consistency issues between Java and Pg (see below).

    IMO you’ll be much better off using traditional case sensitive types and lower(), ILIKE, etc.

    It’s also possible that Hibernate is relying on what PgJDBC tells it about column case sensitivity. At least as of 9.2-devel PgJDBC doesn’t know anything about the citext type, so it’ll always say “yup, that’s case sensitive” when asked.

    Tracing

    It’s hard to be sure that’s what’s happening without seeing the actual queries run by JPA. Try setting log_statement = 'all' in postgresql.conf. Then SIGHUP the postmaster, use pg_ctl reload, or restart Pg to cause the change to take effect.

    Re-run your test and examine the logs. Test the queries you see in psql to observe the results. If you’re unsure what’s going on, update your question with them. If you update also include your Hibernate version and your PgJDBC version.

    It’s also possible that Hibernate is relying on what PgJDBC tells it about column case sensitivity. At least as of 9.2-devel PgJDBC doesn’t know anything about the citext type, so it’ll always say “yup, that’s case sensitive” when asked.

    Operator consistency difficulties

    WARNING: The citext type cannot affect how Hibernate works with the text once it’s come out of the database. It won’t have any effect on the String.equals method, for example. You would need to tell Hibernate you want it to treat the text as case insensitive. Otherwise if you have a text or varchar primary/foreign key you can get situations where the Hibernate asks for the key "FRED", it gets "FrEd" back, and is quite confused because the DB returned a key that isn’t equal – according to Hibernate – to the one it was asked for. Similar oddities will occur if you include citext-backed strings in equals and hashCode implementations in your entities.

    Unfortunately JPA doesn’t seem to specify annotation attributes in the @Column mapping for whether the column is case sensitive or not. Java doesn’t have the concept of a case-insensitive string data type anyway, so it wouldn’t do tons of good even if JPA did specify it.

    You’ll probably avoid confusing Hibernate too badly so long as you don’t use citext for keys or include citext values in equals and hashCode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.