readObjectQuery, check cache
405173Jun 13 2004 — edited Jun 15 2004I created an SQL query in the workbench, "findById", which search the Person table by primary key "ID". It is a ReadObjectQuery. The "Cache Usage" of the query is set to be "Check Cache by Primary Key".
The following test method is used to check whether the query will return result from cache if it is already there.
To my surprise, the second query execution still goes to database to retrieve the record, though the cache has the object.
public void test {
ObjectLevelReadQuery toplinkQuery = null;
String queryName = "findById";
try {
Class targetClass = Person.class;
oracle.toplink.publicinterface.Descriptor descriptor = session.getDescriptor(targetClass);
oracle.toplink.publicinterface.DescriptorQueryManager queryManager = descriptor.getQueryManager();
toplinkQuery = (ObjectLevelReadQuery)queryManager.getQuery(queryName); //a readObjectQuery
System.out.println("getCacheUsage() = " + toplinkQuery.getCacheUsage()); //2 i.e. CheckCacheByPrimaryKey
if (toplinkQuery == null) {
throw new RuntimeException("toplinkQuery is null....");
}
} catch (ClassCastException e) {
throw new RuntimeException("ClassCastException....");
}
Vector v = new Vector();
v.addElement("100");
Person p = (Person)session.executeQuery(toplinkQuery, v);
System.out.println("person's ID = " + p.getId());
System.out.println("person's age = " + p.getAge());
p = (Person)session.executeQuery(toplinkQuery, v);
System.out.println("person's ID = " + p.getId());
System.out.println("person's age = " + p.getAge());
}
Am I missing something here? My understanding is that the readObjectQuery, by default, will check cache first. If the object exists in cache, it will just return it, not going to the db.
Your help will be greatly appreciated.
Haiwei