From 01ea7f07f52d321fc9178c19311a0fed2e60d014 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Thu, 18 May 2017 16:52:44 +0800 Subject: [PATCH] --- src/org/redkale/source/EntityInfo.java | 154 ++++++++++++++++--------- 1 file changed, 102 insertions(+), 52 deletions(-) diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index eb47ccd34..e6555f6ec 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -43,6 +43,15 @@ public final class EntityInfo { //Entity构建器 private final Creator creator; + //Entity构建器参数 + private final String[] constructorParameters; + + //Entity构建器参数Attribute + private final Attribute[] constructorAttributes; + + //Entity构建器参数Attribute + private final Attribute[] unconstructorAttributes; + //主键 final Attribute primary; @@ -202,6 +211,13 @@ public final class EntityInfo { this.tableStrategy = dts; this.creator = Creator.create(type); + Creator.ConstructorParameters cp = null; + try { + cp = this.creator.getClass().getMethod("create", Object[].class).getAnnotation(Creator.ConstructorParameters.class); + } catch (Exception e) { + logger.severe(type + " cannot find ConstructorParameters Creator", e); + } + this.constructorParameters = (cp == null || cp.value().length < 1) ? null : cp.value(); Attribute idAttr0 = null; Map aliasmap0 = null; Class cltmp = type; @@ -271,6 +287,22 @@ public final class EntityInfo { this.queryAttributes = queryattrs.toArray(new Attribute[queryattrs.size()]); this.insertAttributes = insertattrs.toArray(new Attribute[insertattrs.size()]); this.updateAttributes = updateattrs.toArray(new Attribute[updateattrs.size()]); + if (this.constructorParameters == null) { + this.constructorAttributes = null; + this.unconstructorAttributes = null; + } else { + this.constructorAttributes = new Attribute[this.constructorParameters.length]; + List> unconstructorAttrs = new ArrayList<>(); + for (Attribute attr : queryAttributes) { + int pos = Arrays.binarySearch(this.constructorParameters, attr.field()); + if (pos >= 0) { + this.constructorAttributes[pos] = attr; + } else { + unconstructorAttrs.add(attr); + } + } + this.unconstructorAttributes = unconstructorAttrs.toArray(new Attribute[unconstructorAttrs.size()]); + } if (table != null) { StringBuilder insertsb = new StringBuilder(); StringBuilder insertsb2 = new StringBuilder(); @@ -630,61 +662,79 @@ public final class EntityInfo { * @throws SQLException SQLException */ protected T getValue(final SelectColumn sels, final ResultSet set) throws SQLException { - T obj = creator.create(); - for (Attribute attr : queryAttributes) { - if (sels == null || sels.test(attr.field())) { - final Class t = attr.type(); - Serializable o; - if (t == byte[].class) { - Blob blob = set.getBlob(this.getSQLColumn(null, attr.field())); - if (blob == null) { - o = null; - } else { //不支持超过2G的数据 - o = blob.getBytes(1, (int) blob.length()); - } - } else { - o = (Serializable) set.getObject(this.getSQLColumn(null, attr.field())); - if (t.isPrimitive()) { - if (o != null) { - if (t == int.class) { - o = ((Number) o).intValue(); - } else if (t == long.class) { - o = ((Number) o).longValue(); - } else if (t == short.class) { - o = ((Number) o).shortValue(); - } else if (t == float.class) { - o = ((Number) o).floatValue(); - } else if (t == double.class) { - o = ((Number) o).doubleValue(); - } else if (t == byte.class) { - o = ((Number) o).byteValue(); - } else if (t == char.class) { - o = (char) ((Number) o).intValue(); - } else if (t == boolean.class) { - o = (Boolean) o; - } - } else if (t == int.class) { - o = 0; - } else if (t == long.class) { - o = 0L; - } else if (t == short.class) { - o = (short) 0; - } else if (t == float.class) { - o = 0.0f; - } else if (t == double.class) { - o = 0.0d; - } else if (t == byte.class) { - o = (byte) 0; - } else if (t == boolean.class) { - o = false; - } else if (t == char.class) { - o = (char) 0; - } - } + T obj; + Attribute[] attrs = this.queryAttributes; + if (this.constructorParameters == null) { + obj = creator.create(); + } else { + Object[] cps = new Object[this.constructorParameters.length]; + for (int i = 0; i < this.constructorAttributes.length; i++) { + Attribute attr = this.constructorAttributes[i]; + if (sels == null || sels.test(attr.field())) { + cps[i] = getFieldValue(attr, set); } - attr.set(obj, o); + } + obj = creator.create(cps); + attrs = this.unconstructorAttributes; + } + for (Attribute attr : attrs) { + if (sels == null || sels.test(attr.field())) { + attr.set(obj, getFieldValue(attr, set)); } } return obj; } + + protected Serializable getFieldValue(Attribute attr, final ResultSet set) throws SQLException { + final Class t = attr.type(); + Serializable o; + if (t == byte[].class) { + Blob blob = set.getBlob(this.getSQLColumn(null, attr.field())); + if (blob == null) { + o = null; + } else { //不支持超过2G的数据 + o = blob.getBytes(1, (int) blob.length()); + } + } else { + o = (Serializable) set.getObject(this.getSQLColumn(null, attr.field())); + if (t.isPrimitive()) { + if (o != null) { + if (t == int.class) { + o = ((Number) o).intValue(); + } else if (t == long.class) { + o = ((Number) o).longValue(); + } else if (t == short.class) { + o = ((Number) o).shortValue(); + } else if (t == float.class) { + o = ((Number) o).floatValue(); + } else if (t == double.class) { + o = ((Number) o).doubleValue(); + } else if (t == byte.class) { + o = ((Number) o).byteValue(); + } else if (t == char.class) { + o = (char) ((Number) o).intValue(); + } else if (t == boolean.class) { + o = (Boolean) o; + } + } else if (t == int.class) { + o = 0; + } else if (t == long.class) { + o = 0L; + } else if (t == short.class) { + o = (short) 0; + } else if (t == float.class) { + o = 0.0f; + } else if (t == double.class) { + o = 0.0d; + } else if (t == byte.class) { + o = (byte) 0; + } else if (t == boolean.class) { + o = false; + } else if (t == char.class) { + o = (char) 0; + } + } + } + return o; + } }