This is odd since your query is not even using caching, so it hits the
DB and returns the data from there. Do you have Cayenne SQL logging
enabled and does the generated query look like what you expected?
Andrus
On Jun 18, 2010, at 11:15 PM, Lucas Holt wrote:
Earlier today, I was writing some unit tests and stumbled onto a
strange problem with some existing code we have. A result is
getting returned for something that does not exist in the database.
I've included the method that's getting called below. Given a
specific input, we get a valid object even though there is nothing
close to it.
We have a system with a "publications" table and a publication
editions table. The latter is an instance of a publication
typically used when we have a newsletter from a specific date. If
we use today's date and a publication name we'll call A, we get a
PublicationEditions object for publication B on a different date
from last year. The primary key is different as is all the other
meta data for the PublicationEditions object (a cayenne generated
class).
The only possibility I've come up with is that there is some hash
used in the system and we happened to hit a collision. Is this a
possibility? We've been using Cayenne's LRU algorithm, but I've
tried switching to oscache today as well with the same results (also
LRU).
Lucas Holt
public com.prime.pcd.db.PublicationEditions lookup(final String
publicationName, final Date date)
throws IllegalArgumentException, ModelException {
com.prime.pcd.db.PublicationEditions pe = null;
if (publicationName == null || publicationName.length() > 255
|| publicationName.length() < 1) {
throw new IllegalArgumentException("Invalid publication
name");
}
if (date == null) {
throw new IllegalArgumentException("Invalid date");
}
try {
Expression qualifier =
Expression
.fromString("publicationEditionsPublication.publicationName = $pn
and publicationEditionDate=$date");
final HashMap<String, Object> map = new HashMap<String,
Object>(2);
map.put("pn", publicationName);
map.put("date", date);
qualifier = qualifier.expWithParameters(map);
final SelectQuery query = new
SelectQuery(com.prime.pcd.db.PublicationEditions.class, qualifier);
final List<com.prime.pcd.db.PublicationEditions> editions
= dataContext.performQuery(query);
logger.debug( "Number of found items is " +
editions.size());
if (!editions.isEmpty()) {
pe = editions.get(0);
logger.debug(pe.getPublicationEditionsId() + ", " +
pe.getPublicationEditionsPublication().getPublicationName());
}
} catch (CayenneRuntimeException ce) {
logger.error("Cayenne error looking up edition: " +
ce.getMessage());
throw new ModelException("Could not lookup edition");
}
return pe;
}