From 85cb1067f825e6a0134f096c7d25d361d60d7ea6 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 7 Sep 2012 11:02:19 +0200 Subject: [PATCH] [#1802] Result.into(Table) doesn't work correctly, if the same field name appears twice in Result - Improve Record.into(Table) Javadoc --- jOOQ/src/main/java/org/jooq/Record.java | 19 +++++++--- .../main/java/org/jooq/impl/FieldList.java | 36 +++++++------------ 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/Record.java b/jOOQ/src/main/java/org/jooq/Record.java index f0d7e3613e..119fa0db33 100644 --- a/jOOQ/src/main/java/org/jooq/Record.java +++ b/jOOQ/src/main/java/org/jooq/Record.java @@ -1226,11 +1226,20 @@ public interface Record extends FieldProvider, Store { E into(E object) throws MappingException; /** - * Map resulting records onto a custom record type. The mapping algorithm is - * this:

jOOQ will map Record values by equal field names:

- * If a field's value for {@link Field#getName()} is MY_field - * (case-sensitive!), then there must be a field in table with - * the exact same name.

Other restrictions

+ * Map resulting records onto a custom record type. + *

+ * The mapping algorithm is this: + *

jOOQ will map Record values by equal field names:

+ *
    + *
  • For every field in the table argument with + * {@link Field#getName()} "MY_field" (case-sensitive!), a + * corresponding field with the same name in this record will be searched.
  • + *
  • If several fields in this record share the same + * {@link Field#getName()}, then the first one returning true on + * {@link Field#equals(Object)} will be returned. (e.g. qualified field + * names match)
  • + *
+ *

Other restrictions

*
    *
  • {@link Table#getRecordType()} must return a class of type * {@link TableRecord}, which must provide a default constructor. Non-public diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldList.java b/jOOQ/src/main/java/org/jooq/impl/FieldList.java index 1acd9314eb..95181524b3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldList.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldList.java @@ -72,32 +72,22 @@ class FieldList extends QueryPartList> implements FieldProvider { return null; } - Field result = null; - String name = field.getName(); - - for (Field f1 : this) { - if (f1.getName().equals(name)) { - - // Remember the first matching field by name - if (result == null) { - result = f1; - } - - // [#1802] On a colliding second matching field by name, try exact matching - else { - for (Field f2 : this) { - if (f2.equals(field)) { - result = f2; - break; - } - } - - break; - } + // [#1802] Try finding an exact match (e.g. exact matching qualified name) + for (Field f : this) { + if (f.equals(field)) { + return (Field) f; } } - return (Field) result; + // In case no exact match was found, return the first field with matching name + String name = field.getName(); + for (Field f1 : this) { + if (f1.getName().equals(name)) { + return (Field) f1; + } + } + + return null; } @Override