aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to do optional wildcard select in JPA named queries? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to do optional wildcard select in JPA named queries?" Watch "How to do optional wildcard select in JPA named queries?" New topic
Author

How to do optional wildcard select in JPA named queries?

King Lung Chiu
Greenhorn

Joined: Feb 16, 2008
Posts: 8
Hi everyone,

Say I have a Record entity with 3 fields a, b and c, and given a Record instance rec, I use r's fields as criteria for a predefined (named) SELECT query (with AND on all fields).

Does JPQL allow us to write a query that can do wildcard matches on fields that are null?

eg. if rec has all 3 of a, b and c set, then the query would simply be:


But if say field c is null, I'd like to do something like this:


Which should work effectively as this JPQL query:


I want to do wildcard matches because there are quite a number of fields in my entity, and I don't know in advance how many of them (and in which combination) will be supplied in the search criteria. And I'd like to do the above as a named query if possible to avoid having to dynamically construct SQL by hand.

Any idea how to achieve the above in JPQL?

thanks a lot, regards.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
How about using something similar to this?

King Lung Chiu
Greenhorn

Joined: Feb 16, 2008
Posts: 8
Hi,

Thanks for the reply.

When rec.c == null, I want to do a wild-card match, ie return Records with any c value, rather than return records where c == null.

So unfortunately that suggestion doesn't work :-(

Any other thoughts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to do optional wildcard select in JPA named queries?
 
Similar Threads
About URLBirld booking!
Confuse the findByCriteria()
LIKE operator
Complex HQL Query
Regarding apply JOIN by using JPQL.