cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic query syntax for EJB

Former Member
0 Kudos

If i'm going to let the user search the database table (eg. Employee) based on 4 parameters, ie. employee id, employee name, position and address. In cmp entity bean, the stupid way is to create (2 power of 4) findByMethod ejb-ql statements to fulfill the search. Is there any dynamic way to do the querying of table for cmp bean with only one findByMethod?

Any suggestions or help would be much appreciated. Thanks.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

If all the fields are available in your entity bean you can create an oql findbyMethod() and place 4 parameters in your search.

regards,

Dion

Former Member
0 Kudos

Hi Dionisios,

I believe u meant the findByMethod is something like this:

select object(b) from MyEntity b where b.EMPID like ?1 and b.EMPNAME like ?2 and b.POSITION like ?3 and b.ADDRESS like ?4

but what if the user choose to either find by one, two or three parameters, how can at the runtime use the findByMethod which require 4 parameters ?

Former Member
0 Kudos

Hi Julius,

CMP unfortunately does not support dynamic OQL methods (unless something has changed since the last time I looked at the specs). There are however tools and software (eg powertier) that can provide this type of functionality (like a generic findBySQL(String sql) method) on some J2EE servers.

One way to solve it of course is to have 4 different methods, and then in your java code have a case or if statements selecting the correct one based on what the user enters. It is not a pretty solution but it would work.

regards,

Dion