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

  • Home
  • SEARCH
  • 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 1003051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:55:12+00:00 2026-05-16T07:55:12+00:00

Hello guys I am having some problems with exact matches while doing a NamedQuery.

  • 0

Hello guys I am having some problems with exact matches while doing a NamedQuery.

I am currently using something like this:

@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from Entry e where e.name =:"+ Entry.NAME )

...

Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME);
        query.setParameter(Entry.NAME, myEntry.getName());

It works for most cases, however I noticed that in case the user pass the file name with an space at the end, the namedQuery ignores that character. For example:

Query query = em.createNamedQuery(MyClass.GET_ENTRY_BY_NAME);
        query.setParameter(Entry.NAME, myEntry.getName()+ " ");

Will return the same result as the query before. Bypassing my ‘valid entry’ validation. In other words I’d like the query to return no entry at all and treat the error later on.

One workaround I could think of, is to put single quotes surrounding my parameter in the namedQuery, like this:

@NamedQuery(name = MyClass.GET_ENTRY_BY_NAME, query = "select e from entry e where e.name =':"+ Entry.NAME "'")

However it will trash my code in case the String contains single quotes in it…

Any ideas guys?

  • 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-05-16T07:55:13+00:00Added an answer on May 16, 2026 at 7:55 am

    I did some research in JPA and found out that it does some automatic trimming for CHARs, I am not sure if this behaves the same with Strings, but since it is happening to me… I believe so. The only way to bypass it is by setting some attribute within the session DatabaseLogin object (see http://www.eclipse.org/eclipselink/api/1.1/org/eclipse/persistence/sessions/DatabaseLogin.html#setShouldTrimStrings) .

    Well I didn’t want to be messing up with the session properties so I decided to make some sort of check and throwing the same exception as the NoResultException catch does in my code.

    I basically took the result from the database and compared the field with the String I used:

    query.setParameter(Entry.NAME, myEntry.getName());
    
    ...
    
    if(!StringUtils.equals(result.getName(), myEntry.getName()){
       do a cool throw just like NoResultException Catch
    }
    

    I also had to include the Trim function axtavt! This is just to make sure that if the database has a column with trailing spaces and it matches the parameter given by the user, it will be included as a valid answer. For example:

    Database entry: Name = “Flavio ” – Trimmed with Function = “Flavio”.

    Parameter passed: Name = “Flavio ” – Trimmed by JPA automatic function = “Flavio”.

    If it isnt trimmed at all it will just Compare “Flavio ” with “Flavio”, returning NoResult when it was supposed to return that Entry.

    Nasty workaround, but as long as there is no other way to stop the auto-trimming we will have to just make use of this sort of things.

    Thanks for all the other answers!!

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

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer int textBoxCount = controlsList.Count(o => (int)o[0] == 1); int checkBoxCount… May 16, 2026 at 10:43 am
  • Editorial Team
    Editorial Team added an answer AIR 2 came out after Flash Builder 4, as such… May 16, 2026 at 10:43 am
  • Editorial Team
    Editorial Team added an answer I got errors like this a while back, and it… May 16, 2026 at 10:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hello guys can anyone help me with the issue im having with installation of
Hello i have recently started using vim editor ( actually the plugin for netbeans
I'm having problems when trying to compile the following code: I first tried compiling
This is my first post on the forum, hope all of you guys are
Hey guys, I am having trouble with what seems to be fairly easy. Let
I am trying to design some simple GUIs with Glade to use with Ruby.
Hello all Windows Mobile Experts! I have really drowned in the world of developing
Hi guys I have a google apps account set up and even set up
I expected the code below to print Hello, world! every 5 seconds, but what
Is it possible to attach html tag to others using Javascript? <li> to other

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.