Quantcast

How do I map a numeric column to a Long?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How do I map a numeric column to a Long?

jonwa
Note that I said "Long" not "long".

A numeric database column has the potential to be null, and thus a
nullable column ought be mapped to a Long so that you can maintain the
knowledge of null and not assume the knowledge of zero.

I tried specifying the jdbc type as "NUMERIC" and the java field is of
type Long, but instead of geting a pojo with a null value for the Long
field I get a stack trace with
java.sql.SQLException: Fail to convert to internal representation
at the top.

Do I really have to map everything to a String?

I could do this everywhere, but it's really lame.

        private String fieldAsString;

        public Long getField() {
                return fieldAsString == null ? null : Long.parseLong(fieldAsString);
        }


I'm firmly of the belief that ResultSet.getInt() ought to have
returned a Long in the first place, silly JDBC.

Or better yet:
ResultSet.getInt() would return an int for not nullable columns
and
ResultSet.getInteger() would return an Integer for nullable columns.

Sometimes you need to know that you don't know.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How do I map a numeric column to a Long?

Jeff Butler
This should work - the generator defaults to generating code like this.

But this is driver dependent.  What is the DB?  What is the actual
type on the DB side?

Jeff Butler

On Thu, Mar 15, 2012 at 6:08 PM, jonwa <[hidden email]> wrote:

> Note that I said "Long" not "long".
>
> A numeric database column has the potential to be null, and thus a
> nullable column ought be mapped to a Long so that you can maintain the
> knowledge of null and not assume the knowledge of zero.
>
> I tried specifying the jdbc type as "NUMERIC" and the java field is of
> type Long, but instead of geting a pojo with a null value for the Long
> field I get a stack trace with
> java.sql.SQLException: Fail to convert to internal representation
> at the top.
>
> Do I really have to map everything to a String?
>
> I could do this everywhere, but it's really lame.
>
>        private String fieldAsString;
>
>        public Long getField() {
>                return fieldAsString == null ? null : Long.parseLong(fieldAsString);
>        }
>
>
> I'm firmly of the belief that ResultSet.getInt() ought to have
> returned a Long in the first place, silly JDBC.
>
> Or better yet:
> ResultSet.getInt() would return an int for not nullable columns
> and
> ResultSet.getInteger() would return an Integer for nullable columns.
>
> Sometimes you need to know that you don't know.
Loading...