Corporate Application Templates , 2012 edition. A question about code examples in the Data Mapper chapter.

The same load () method of the AbstractMapper class in the chapter is presented three times with one change, which is confusing.

So, the code on page 194:

protected DomainObject load(ResultSet rs) throws SQLException { Long id = new Long(rs.getLong(1)); if (loadedMap.containsKey(id)) return (DomainObject) loadedMap.get(id); DomainObject result = doLoad(id, rs); loadedMap.put(id, result); return result; } 

The same method (but with "id" instead of one) on page 200:

 protected DomainObject load(ResultSet rs) throws SQLException { Long id = new Long(rs.getLong("id")); //... } 

On page 201 (within the book reversal) the unit is again:

 protected DomainObject load(ResultSet rs) throws SQLException { Long id = new Long(rs.getLong(1)); //... } 

What should be passed to the rs.getLong () method?

  • The ResultSet.getLong () method is, as it were, described in the java.sql documentation, and it’s as if written there, what arguments it takes ... - PinkTux
  • Good. Do I understand correctly that the 1 in these examples is the magic number ? - Denis Khvorostin 1:43 pm
  • one
    You can say so. But this is at least a controversial issue: the numbers 0, 1, and 2 are often considered to be permissible to use explicitly, especially in self-evident contexts. - PinkTux

2 answers 2

What should be passed to the rs.getLong () method?

What you need in each specific context. For:

ResultSet Members

...

getlong (int)

getlong (String)

...

    Methods to get column values ​​for the specified row, getters (getBoolean (), getLong (), getInt (), etc.), can take either the index (sequence number) of a column in integer form, or the string that corresponds to the column name.

    In this example, for Fowler, it is logical to assume that he simply has the "id" column going first, i.e. has index 1. Accordingly, in his examples he regards these records as identical and interchangeable.

    • Igor, I would not be in a hurry with the phrases "it is logical to assume", "considers as identical." First, the book has real inconsistencies and problems with the code. Secondly, the book is transferable and this is another source of potential errors. Fowler, of course, is cool, but the use of magic numbers in a code dedicated to patterns is so-so. - Denis Khvorostin
    • I would not say that the 1 here is the magic number. With the same "success" you can find fault with the "id" . - PinkTux
    • The code in the book is illustrations to the theory, not examples of real code, ready to use, and not blanks, from which it is worth dancing - Igor Karpenko
    • 1. About magic numbers: even if formally it is not it, it is a very close and less user-friendly solution. 2. About “finding fault with ID”: the question is the question: why do the data dance? 3. About the role of the code in the book - I do not agree: he is here precisely in order to have something to dance from. - Denis Khvorostin
    • Do you seriously consider the code given in the book, as an example of a real working for the production of a solution that is sufficiently well read, replaced with your tables / constants / entities, slightly rewritten and used in battle? these are nothing more than examples that illustrate the material described in the chapters for informational purposes, of which it is enough to catch the idea, and not to study the implementation in detail - Igor Karpenko