Hi Charles,

>Criteria crit = new Criteria();
>if(!this.getName()==null){
>  crit.addEqualTo("name", this.getName());
>}
>if(!this.getDhcp()==null){
>  crit.addEqualTo("dhcp", this.getDhcp());
>}
>if(!this.this.getStatus()==null){
>  crit.addEqualTo("status", this.getStatus());
>}

I think that's the best solution too.

Thanks
Regards
Sylvain

-----Message d'origine-----
De: Charles Anthony [mailto:[EMAIL PROTECTED]
Date: vendredi, 28. mars 2003 11:14
À: 'OJB Users List'
Objet: RE: How to manage criteria entries


No (don't forget, in most databases you can only do wildcard queries on
CHAR/VARCHAR fields)

In your example, only "name" can have no value. (boolean and int are
primitives, therefore they always have a value.)

You could do :
Criteria crit = new Criteria();
if(!this.getName()==null){
        crit.addEqualTo("name", this.getName());
}
crit.addEqualTo("dhcp", new Boolean(this.getDhcp()));
crit.addEqualTo("status", new Integer(this.getStatus()));

Or
Criteria crit = new Criteria();
String searchString = this.getName() + "%";
crit.addLike("name", searchString);
crit.addEqualTo("dhcp", new Boolean(this.getDhcp()));
crit.addEqualTo("status", new Integer(this.getStatus()));

I myself would change the boolean and int primitives on the class to Boolean
and Integer, and do


Criteria crit = new Criteria();
if(!this.getName()==null){
  crit.addEqualTo("name", this.getName());
}
if(!this.getDhcp()==null){
  crit.addEqualTo("dhcp", this.getDhcp());
}
if(!this.this.getStatus()==null){
  crit.addEqualTo("status", this.getStatus());
}

HTH,

Cheers,

Charles.
>-----Original Message-----
>From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
>Sent: 28 March 2003 10:08
>To: [EMAIL PROTECTED]
>Subject: How to manage criteria entries
>
>
>Hello,
>
>I'm working on a Search web form and I'm using OJB to retrieve 
>data from my database.
>
>I have three criteria on my form:
>- name (String)
>- dhcp (boolean)
>- status (int)
>
>The user has 2 choices: enter a data in the form fields or not.
>
>I want to retrieve:
>-> all (no criteria in the query) if there is nothing in the 
>form field.
>-> the data considering the criteria entered in the form field 
>if there is one.
>
>The code is
>----code----
>Criteria crit = new Criteria();
>crit.addEqualTo("name", this.getName());
>crit.addEqualTo("dhcp", new Boolean(this.getDhcp()));
>crit.addEqualTo("status", new Integer(this.getStatus()));
>
>Query query = new QueryByCriteria(Workstation.class, crit);
>
>Collection toBeEdited = broker.getCollectionByQuery(query);
>...
>----code----
>
>A problem occurs when there are nothing in the form fields, 
>then OJB try to find data that have nothing in their fields 
>instead of all the data.
>
>Is there an "OJB" solution to do:
>
>if field = ""
>  then field = "%"
>
>
>Thanks
>Sylvain
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>


This email and any attachments are strictly confidential and are intended
solely for the addressee. If you are not the intended recipient you must
not disclose, forward, copy or take any action in reliance on this message
or its attachments. If you have received this email in error please notify
the sender as soon as possible and delete it from your computer systems.
Any views or opinions presented are solely those of the author and do not
necessarily reflect those of HPD Software Limited or its affiliates.

 At present the integrity of email across the internet cannot be guaranteed
and messages sent via this medium are potentially at risk.  All liability
is excluded to the extent permitted by law for any claims arising as a re-
sult of the use of this medium to transmit information by or to 
HPD Software Limited or its affiliates.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to