Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB-QL question

 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using EJB 2.1. I want to write a query that returns objects matching an Integer parameter, but if the passed in parameter is null, returns objects with null in that field. Something like:


The only problem is "?1 IS NULL" is illegal. Is there any other way I can do it, or do I have to write two different queries?
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,
You can't make run time check or can't make use of EQL functions to evaluate EJB QL parameters i.e. parameters those are passed to finder methods as arguments.
Actually it does not make sence coz objective of EQL is to reterive the data from database as per EQL , not to perform any type of check on finder method's parametrs.
These check should me performed perior to calling the finder method.
So method which is calling that finder method has to make this check.
SO you need to create to finders

1. SELECT OBJECT(o) from MyTable WHERE o.myField = ?1

2. SELECT OBJECT(o) from MyTable WHERE o.myField IS NULL

call finder 1 when the passed parameter is not null otherwise call finder 2

Thnaks
Sunil Dixit
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sunil. It makes some sense, but in my case myField is a unique key, which could be null or an integer. It would be nice to be able to retrieve the record with that unique key in a single query. If it's not possible though, then it's not possible.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic