You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Sy...@swisscom.com on 2002/12/11 13:37:55 UTC
[PB] how to retrieve one entry
Hello,
I have a class like:
class Workstation {
int workstationID
String workstationName
...
}
The workstation attribute is the primary key (autoincrement=true).
I want to enter the workstationName and retrieve the corresponding entry.
If I try this:
----code
Workstation workstation = new Workstation();
workstation.setWorkstationName("Test");
Query query = new QueryByExample(workstation);
try {
broker.beginTransaction();
Workstation toBeEdited = (Workstation) broker.getObjectByQuery(query);
broker.commitTransaction();
} catch (Throwable t) {
broker.abortTransaction();
t.printStackTrace();
}
----code
This code doesn't work because OJB create a new primary key for this instance and try to find it in the database. Of course it doesn't find it and the query return a null value.
It seems that the only way to retrieve one entry is by the workstationID.
Is it right?
Is there other method to do this?
Thank you
Sylvain
SV: [PB] how to retrieve one entry
Posted by Johan Neidenmark <jo...@e-man.se>.
Criteria crit = new Criteria();
crit.addEqualTo("workstationName", "Test");
Query query = new QueryByCriteria(Workstation.class, crit, true); //
true to get distinct, just in case
Workstation toBeEdited = (Workstation) broker.getObjectByQuery(query);
Is one way to do it..
/Johan
-----Ursprungligt meddelande-----
Från: Sylvain.Thevoz@swisscom.com [mailto:Sylvain.Thevoz@swisscom.com]
Skickat: den 11 december 2002 13:38
Till: ojb-user@jakarta.apache.org
Ämne: [PB] how to retrieve one entry
Hello,
I have a class like:
class Workstation {
int workstationID
String workstationName
...
}
The workstation attribute is the primary key (autoincrement=true).
I want to enter the workstationName and retrieve the corresponding
entry. If I try this: ----code Workstation workstation = new
Workstation(); workstation.setWorkstationName("Test");
Query query = new QueryByExample(workstation);
try {
broker.beginTransaction();
Workstation toBeEdited = (Workstation) broker.getObjectByQuery(query);
broker.commitTransaction(); } catch (Throwable t) {
broker.abortTransaction(); t.printStackTrace(); } ----code
This code doesn't work because OJB create a new primary key for this
instance and try to find it in the database. Of course it doesn't find
it and the query return a null value.
It seems that the only way to retrieve one entry is by the
workstationID. Is it right? Is there other method to do this?
Thank you
Sylvain
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>