Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JOOQ restricted to Integer values and not Longs?

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

like image 855
George Pligoropoulos Avatar asked Nov 26 '13 13:11

George Pligoropoulos


People also ask

How does jOOQ work in Java?

As jOOQ is just a wrapper around JDBC, Java database basics apply and you need to get a database connection for jOOQ to work. You either open one yourself, or ask your connection pool to give you one. Let’s have a look at a jOOQ DSL example, using Java’s DriverManager. What’s happening here? You open up a database connection with plain JDBC.

How is jOOQ different from other Orms?

Unlike most other ORMs, jOOQ is relational model-centric and not domain model-centric. Hibernate, for example, helps us to write Java code that is then automatically translated to SQL. However, jOOQ allows us to create relational objects in the database using SQL, and it then generates the Java code to map to those objects.

What is the difference between jOOQ and hibernate?

Compared to other popular libraries like Hibernate, jOOQ takes a database-first or SQL-centric approach. With Hibernate, you usually start writing your Java classes first and then let Hibernate or a tool like Liquibase or Flyway generate corresponding database tables. With jOOQ, you start with your database.

What is jOOQ Autoconfiguration in Spring Boot?

Spring Boot comes with a jOOQ autoconfiguration, which means it sets up the DSLContext and integrates jOOQ with Spring’s transaction handling for you - without you having to do anything apart from adding the following dependency to your Spring Boot project. Spring Boot creates the DSLContext automatically for you (dependent on a datasource).


1 Answers

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());
}
like image 76
Lukas Eder Avatar answered Oct 14 '22 10:10

Lukas Eder