Fix for #227 - Many2Many cache bug, incorrect query plan hash calculation

This commit is contained in:
rbygrave
2015-01-11 20:45:07 +13:00
parent a998c0be55
commit 961b219e3e
3 changed files with 44 additions and 4 deletions
@@ -50,6 +50,11 @@ public final class TableJoin {
*/
private final TableJoinColumn[] columns;
/**
* A hash that can be used with the query plan.
*/
private final int queryHash;
/**
* Create a TableJoin.
*/
@@ -77,7 +82,26 @@ public final class TableJoin {
BeanProperty prop = propMap.get(deployProps[i].getName());
this.properties[i] = prop;
}
this.queryHash = calcQueryHash();
}
/**
* Calculate a hash value for adding to a query plan.
*/
private int calcQueryHash() {
int hc = type.hashCode();
hc = hc * 31 + (table == null ? 0 : table.hashCode());
for (int i = 0; i < columns.length; i++) {
hc = hc * 31 + columns[i].queryHash();
}
return hc;
}
/**
* Return a hash value for adding to a query plan.
*/
public int queryHash() {
return queryHash;
}
public String toString() {
@@ -21,7 +21,12 @@ public class TableJoinColumn {
private final boolean insertable;
private final boolean updateable;
/**
* Hash for including in a query plan
*/
private final int queryHash;
/**
* Create the pair.
*/
@@ -30,12 +35,23 @@ public class TableJoinColumn {
this.foreignDbColumn = InternString.intern(deploy.getForeignDbColumn());
this.insertable = deploy.isInsertable();
this.updateable = deploy.isUpdateable();
this.queryHash = hashOf(localDbColumn) * 31 + hashOf(foreignDbColumn);
}
private int hashOf(String value) {
return (value == null) ? 0 : value.hashCode();
}
public String toString() {
return localDbColumn+" = "+foreignDbColumn;
}
/**
* Return a hash for including in a query plan.
*/
public int queryHash() {
return queryHash;
}
/**
* Return the foreign database column name.
@@ -666,8 +666,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
*/
private HashQueryPlan calculateHash(BeanQueryRequest<?> request, HashQueryPlanBuilder builder) {
// exclude bind values and things unrelated to
// the sql being generated
// exclude bind values and things unrelated to the sql being generated
if (builder == null) {
builder = new HashQueryPlanBuilder();
@@ -680,6 +679,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
builder.add(mapKey);
builder.add(id != null);
builder.add(rawSql == null ? 0 : rawSql.queryHash());
builder.add(includeTableJoin != null ? includeTableJoin.queryHash() : 0);
if (detail != null) {
detail.queryPlanHash(request, builder);