This commit is contained in:
地平线
2015-10-30 11:49:35 +08:00
parent 3109910263
commit 2c7d8f64d4
2 changed files with 17 additions and 10 deletions

View File

@@ -82,15 +82,17 @@ final class FilterBeanNode extends FilterNode {
if (secinfo.getCache() == null || !secinfo.getCache().isFullLoaded()) { if (secinfo.getCache() == null || !secinfo.getCache().isFullLoaded()) {
joinallcached = false; joinallcached = false;
} }
final String jc = joinCol.column().isEmpty() ? secinfo.getPrimary().field() : joinCol.column();
if (first) { if (first) {
joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable()) joinsb.append(" ").append(joinCol.type().name()).append(" JOIN ").append(secinfo.getTable())
.append(" ").append(alias).append(" ON a.# = ").append(alias).append(".") .append(" ").append(alias).append(" ON a.# = ").append(alias).append(".")
.append(joinCol.column().isEmpty() ? secinfo.getPrimarySQLColumn() : secinfo.getSQLColumn(joinCol.column())); .append(secinfo.getSQLColumn(jc));
} }
newnode.foreignEntity = secinfo; newnode.foreignEntity = secinfo;
newnode.tabalis = alias; newnode.tabalis = alias;
newnode.columnAttribute = secinfo.getAttribute(newnode.column); newnode.columnAttribute = secinfo.getAttribute(newnode.column);
newnode.foreignAttribute = joinCol.column().isEmpty() ? secinfo.getPrimary() : secinfo.getAttribute(joinCol.column()); newnode.byjoinColumn = jc;
newnode.foreignAttribute = secinfo.getAttribute(jc);
if (newnode.foreignEntity != null && newnode.foreignAttribute == null) throw new RuntimeException(clazz.getName() + "." + field.getName() + " have illegal FilterJoinColumn " + joinCol); if (newnode.foreignEntity != null && newnode.foreignAttribute == null) throw new RuntimeException(clazz.getName() + "." + field.getName() + " have illegal FilterJoinColumn " + joinCol);
} }
} }
@@ -139,9 +141,11 @@ final class FilterBeanNode extends FilterNode {
//--------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------
private Attribute beanAttribute; private Attribute beanAttribute;
private EntityInfo foreignEntity; private EntityInfo foreignEntity; // join 表
private Attribute foreignAttribute; private String byjoinColumn; //被join表的join字段
private Attribute foreignAttribute; //join表的join字段
private Attribute columnAttribute; private Attribute columnAttribute;
@@ -196,6 +200,7 @@ final class FilterBeanNode extends FilterNode {
newnode.express = this.express; newnode.express = this.express;
newnode.nodes = this.nodes; newnode.nodes = this.nodes;
newnode.foreignEntity = this.foreignEntity; newnode.foreignEntity = this.foreignEntity;
newnode.byjoinColumn = this.byjoinColumn;
newnode.foreignAttribute = this.foreignAttribute; newnode.foreignAttribute = this.foreignAttribute;
newnode.columnAttribute = this.columnAttribute; newnode.columnAttribute = this.columnAttribute;
newnode.array = this.array; newnode.array = this.array;
@@ -213,6 +218,7 @@ final class FilterBeanNode extends FilterNode {
FilterBeanNode beanNode = ((FilterBeanNode) node); FilterBeanNode beanNode = ((FilterBeanNode) node);
this.beanAttribute = beanNode.beanAttribute; this.beanAttribute = beanNode.beanAttribute;
this.foreignEntity = beanNode.foreignEntity; this.foreignEntity = beanNode.foreignEntity;
this.byjoinColumn = beanNode.byjoinColumn;
this.foreignAttribute = beanNode.foreignAttribute; this.foreignAttribute = beanNode.foreignAttribute;
this.columnAttribute = beanNode.columnAttribute; this.columnAttribute = beanNode.columnAttribute;
this.array = beanNode.array; this.array = beanNode.array;
@@ -228,7 +234,7 @@ final class FilterBeanNode extends FilterNode {
protected <T> StringBuilder createFilterSQLExpress(final boolean first, final EntityInfo<T> info, FilterBean bean) { protected <T> StringBuilder createFilterSQLExpress(final boolean first, final EntityInfo<T> info, FilterBean bean) {
if (joinSQL == null || !first) return super.createFilterSQLExpress(first, info, bean); if (joinSQL == null || !first) return super.createFilterSQLExpress(first, info, bean);
StringBuilder sb = super.createFilterSQLExpress(first, info, bean); StringBuilder sb = super.createFilterSQLExpress(first, info, bean);
String jsql = joinSQL.replace("#", info.getPrimarySQLColumn()); String jsql = joinSQL.replace("#", info.getSQLColumn(byjoinColumn));
return new StringBuilder(sb.length() + jsql.length()).append(jsql).append(sb); return new StringBuilder(sb.length() + jsql.length()).append(jsql).append(sb);
} }
@@ -280,23 +286,24 @@ final class FilterBeanNode extends FilterNode {
} }
} }
if (foreign.isEmpty()) return result; if (foreign.isEmpty()) return result;
final String byjoinCol = this.byjoinColumn;
final Attribute foreignAttr = this.foreignAttribute; final Attribute foreignAttr = this.foreignAttribute;
for (final Map.Entry<EntityInfo, Predicate> en : foreign.entrySet()) { for (final Map.Entry<EntityInfo, Predicate> en : foreign.entrySet()) {
Attribute<T, Serializable> mainIdAttr = info.getPrimary(); Attribute<T, Serializable> byjoinAttr = info.getAttribute(byjoinCol);
final EntityCache cache = en.getKey().getCache(); final EntityCache cache = en.getKey().getCache();
final Predicate p = en.getValue(); final Predicate p = en.getValue();
Predicate<T> f = new Predicate<T>() { Predicate<T> f = new Predicate<T>() {
@Override @Override
public boolean test(T t) { public boolean test(T t) {
Serializable key = mainIdAttr.get(t); Serializable key = byjoinAttr.get(t);
Predicate k = (e) -> key.equals(foreignAttr.get(e)); Predicate k = (e) -> key.equals(foreignAttr.get(e));
return cache.exists(k.and(p)); return cache.exists(k.and(p));
} }
@Override @Override
public String toString() { public String toString() {
return "(" + mainIdAttr.field() + " = " + en.getKey().getType().getSimpleName() + "." + foreignAttr.field() + " AND " + p + ")"; return "(" + byjoinAttr.field() + " = " + en.getKey().getType().getSimpleName() + "." + foreignAttr.field() + " AND " + p + ")";
} }
}; };
final Predicate<T> one = result; final Predicate<T> one = result;

View File

@@ -25,14 +25,14 @@ public @interface FilterJoinColumn {
} }
/** /**
* 关联表 * 关联表 通常join表默认别名为b/c/d/...自增, 被join表默认别名为a
* *
* @return * @return
*/ */
Class table(); Class table();
/** /**
* 默认使用主键 * 默认使用join表(b)的主键, join表与被join表(a)的字段必须一样
* <p> * <p>
* @return * @return
*/ */