Why is JOOQ restricted to Integer values and not Longs?

integer, java, jooq, long-integer, sql

Solution

In your database, `LOGGED_IN.USER_ID` is probably of the SQL type `INT` which has its best equivalence in a Java `Integer` or a Scala `Int`. If you wanted to operate on a `Long`, you should change your database column's type to `BIGINT`.

There's no way around this "limitation", which is a good thing in my opinion. For instance, you cannot insert a `Long` into an `INT` database column. With jOOQ, the Java / Scala compilers will prevent that from happening accidentally.

Workaround using implicit conversion

There is a workaround in Scala for this kind of problem. You can, of course try to apply implicit conversion by extending the existing jOOQ-scala tools.

trait SNumberField[T <: Number] extends SAnyField[T] {
  // [...]
  def equal(value : Number)        : Condition;
  def equal(value : Field[Number]) : Condition;
}

abstract class NumberFieldBase[T <: Number](override val underlying: Field[T])
    extends AnyFieldBase[T] (underlying)
    with SNumberField[T] {

  // [...]
  def equal(value : Number)
    = underlying.equal(underlying.getDataType().convert(value));
  def equal(value : Field[_ <: Number]) 
    = underlying.equal(value.coerce(underlying.getDataType());
}

Problem

I am new with JOOQ and it seems a little strange why it has many of my columns type-safed as Integers while they could easily need to be Longs in the near future. Even `count(*)` results must be casted into Integers! Is there a setting to have Long as default or any way to set Long globally in current project? (wherever this may apply) If this is not possible.. Is there a reason to have it as Integer?' this is a sample of some code in `Scala`: ``` def loggedInUserOwnsAccount(userId: Long) = { selectCount(). from(LOGGED_IN, EMAIL_ACCOUNT). where(LOGGED_IN.USER_ID.equal(EMAIL_ACCOUNT.PASS_ID)). and(LOGGED_IN.USER_ID.equal(userId.toInt)).asInstanceOf[ResultQuery[Record]] } ``` Please note that the important part is that I need to convert Long to Int with this code `userId.toInt`, otherwise it will not compile

Original source

Related problems