How to use LIKE operator in Active Objects query

Hallo :-)

i would like to fetch all records in DB that have similar titles with LIKE operator. My query looks like:

import com.atlassian.activeobjects.external.ActiveObjects;
...
private final ActiveObjects ao;
...
int queryResult0 = ao.find(CategoryEntity.class,
                Query.select().where("CATEGORY_TITLE LIKE ?", categoryTitle)).length;

but i allways get an empty set!!! only if the category title exactly matches, like MyTitle -> MyTitle (TRUE) i get some results if not, like MyTitle - mytitle (FALSE) i get no results!

Please help :-)

Thanks

Max

3 answers

1 accepted

This widget could not be displayed.

it was very easy... CATEGORY_TITLE and "query" string should be casted to lower or upper case:

int queryResult0 = ao.find(CategoryEntity.class,
                Query.select().where("LOWER(CATEGORY_TITLE) LIKE LOWER(?)", "%" + categoryTitle + "%")).length;

because like- query is case sensetive and you should have two strings in same, lower or upper case to compare....

Thanks!

This widget could not be displayed.
You should write ...LIKE '%?%'
Ok, it makes sense :-) i will try it out

i tried it out and my query looks like this one:

int queryResult0 = ao.find(CategoryEntity.class,
                Query.select().where("CATEGORY_TITLE LIKE '%?%'", categoryTitle)).length;

but i got exception:

categoryTitle : management
caused by: com.atlassian.activeobjects.internal.ActiveObjectsSqlException: There was a SQL exception thrown by the Active Objects library:
Database:
  - name:HSQL Database Engine
  - version:2.2.4
  - minor version:2
  - major version:2
Driver:
  - name:HSQL Database Engine Driver
  - version:2.2.4

java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 1
at com.atlassian.activeobjects.internal.EntityManagedActiveObjects.find(EntityManagedActiveObjects.java:165)
caused by: java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 1
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
caused by: org.hsqldb.HsqlException: Invalid argument in JDBC call: parameter index out of range: 1
at org.hsqldb.error.Error.error(Unknown Source)

This widget could not be displayed.
Jason Hinch Atlassian Team Aug 13, 2013

You need to concatenate the % with the search text:

int num = ao.count(CategoryEntity.class,
  Query.select()
    .where("CATEGORY_TITLE LIKE ?",
      "%" + categoryTitle + "%"));

Unfortunately this does not work too. I have adapted the query to:

int queryResult1 = ao.count(CategoryEntity.class,
   Query.select().where("CATEGORY_TITLE LIKE ?",        
   "%"+categoryTitle+"%"));

i have one record in db: Management and i try to test the query and find management or mAnagement but i get allways an empty set back :-(

Hi guys,

I have a similar problem too. I implemented Max solution it works inMemory database but when using PostgreSql does not work. Is there a work around for PostGreSql when we come to compare without checking the case?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 17, 2018 in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

595 views 7 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you