Hi Darren,

I vaguely recall having discussed this on the user group before, but I
cannot seem to find the relevant thread...
The behaviour that you have observed is specified by the
DefaultRecordMapper, which defines the behaviour of all "into(Class)"
methods:
http://www.jooq.org/javadoc/latest/org/jooq/impl/DefaultRecordMapper.html

In the case of java.lang.Long, this rule applies:

If no default constructor is available, but at least one "matching"
constructor is available, that one is used


   - A "matching" constructor is one with exactly as many arguments as this
      record holds fields
      - When several "matching" constructors are found, the first one is
      chosen (as reported by
Class.getDeclaredConstructors()<http://java.sun.com/javase/6/docs/api/java/lang/Class.html?is-external=true#getDeclaredConstructors()>
      - When invoking the "matching" constructor, values are converted onto
      constructor argument types


In the case of java.lang.String, there is a default constructor String(),
which is used, and which explains why you get empty strings as a result.

The primitive type use-case is currently not handled correctly. I wonder if
we should indeed adapt the DefaultRecordMapper in the event of mapping
Record1 types to any "well-known" type (i.e. a type available from
SQLDataType:
http://www.jooq.org/javadoc/latest/org/jooq/impl/SQLDataType.html), or to a
type available from a registered Converter. Certainly, these value-types
shouldn't be handled the same way as POJO types, even if there is no formal
way to distinguish them...

I think we can implement this for jOOQ 3.4:
https://github.com/jOOQ/jOOQ/issues/3212

Note, while it would be possible to pass a primitive type, e.g. as
"fetchInto(int.class)", this would still return a List<Integer> due to the
way generics work. The type of int.class is Class<Integer>:
http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#TYPE. The
way jOOQ currently handles primitive types when using type conversion is
that nulls are converted to the initialisation value of the respective
primitive type. E.g. 0 for int, 0.0 for double, false for boolean, etc.

Cheers
Lukas

2014-04-26 16:08 GMT+02:00 Darren S <[email protected]>:

> I ran into a bug in my code because I was using jOOQ wrong, but thought
> maybe it would be nice to fail on this situation.
>
> I wanted to select a single column that was a long and instead of writing
> fetch(TABLE.FIELD), as I should, I wrote fetchInto(Long.class).  And it
> worked.  So I thought you could just put a primative type for fetchInto().
> Later I wanted to select a string column, so I wrote
> fetchInto(String.class) which does not work.  It will just return a blank
> string for every entry.  So digging a bit into this I found out that
> fetchInto(primitive type) is not supported but Long just happens to work
> because it has a Long(long) constructor that by coincidences is the first
> constructor returned.
>
> So it might be nice in jOOQ that if somebody passes a primitive type it
> fails or actually handles it.
>
> Darren
>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to