Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

EJB QL ....where o.parameter1 = *

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

im not very experienced in EJB QL, and i have a small problem. Im developing an "advance search" (when the primary key is not known) where people can fill in what they do know about a particular database entry.

So... when I write my EJB-QL query for my ejbSelect method, it gets tricky. The problem is that not all parameters will be filled in by the user. So I cant write my EJB-QL query like:
SELECT OBJECT (o) FROM TestSchema o WHERE o.parameter1 = ?1 AND o.parameter2 = ?2 AND o.parameter3 = ?3 AND o.parameter4 = ?4

Those parameters that arn't filled in by the user... how can I make my EJB-QL query to understand that i dont want any filtration of that particular parameter? In SQL you could simply replace an empty string with "*". But you cant do that in EJB-QL. Any suggestions?

[ February 03, 2007: Message edited by: Kalle Anka ]

[ February 03, 2007: Message edited by: Kalle Anka ]
[ February 03, 2007: Message edited by: Kalle Anka ]
 
author & internet detective
Posts: 40472
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kalle,
This is significantly easier to do using pure JDBC rather than EJB QL. In JDBC, you can build up the SQL string at runtime with just the parameters you need.
 
Kalle Anka
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jeanne,

by JDBC you mean writing SQL queries directly to the database right? Wouldnt I then need to open a database connection in my entity bean? And wouldnt that be considered to be bad programming when your using J2EE?

If using JDBC... should i change my ejbSelectXXX to another method ejbWhatever and where I make my sql query?
 
Jeanne Boyarsky
author & internet detective
Posts: 40472
821
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kalle Anka:
by JDBC you mean writing SQL queries directly to the database right?


Yes.

Wouldnt I then need to open a database connection in my entity bean? And wouldnt that be considered to be bad programming when your using J2EE?


You wouldn't do this from within the EJB. You would have whomever is calling the EJB finder call the JDBC class instead. So the JDBC code goes in a new class (typically called a DAO - data access object.) Then your servlet or session bean (or whomever is calling the entity bean now) calls the DAO.

It would be considered bad programming to call JDBC within an entity bean. However, JDBC is not prohibited or even discouraged when using J2EE. There is no rule or best practice stating you must limit yourself to entity beans.

[edited to fix quotes]
[ February 04, 2007: Message edited by: Jeanne Boyarsky ]
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For EJB 2.x, the best overall architecture is to not use Entity Beans, hence also no EJB-QL, but use an ORM tool, like Hibernate, Ibatis, or TopLink, they have much more expressive querying language to allow such a query. You should probably avoid JDBC because of how much code it takes to do your work. It will consume about 30% of your code, whereas an ORM tool will do most of your work under the covers.


For EJB 3.0 Entity Beans are now part of the JPA Java Persistence API/Architecture, and is basically a specification for an ORM tool. And would be the route to go.

Mark
 
I do some of my very best work in water. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic