简体   繁体   中英

JOOQ forced type code generation

I'm having some problems generating code using forced types (JOOQ 3.3, Postgres 9.3).

Trying to convert sql timestamp to joda DateTime, leads me to compilation errors.

My table:

CREATE TABLE book
(
  // [...]
  date_published timestamp without time zone,
  // [...]
);

and my .xml config:

// [...]
<customTypes>
  <customType>
   <name>java.sql.Timestamp</name>
   <converter>com.plannow.jooq.converters.DateTimeConverter</converter>
  </customType>         
</customTypes>
<forcedTypes>
  <forcedType>
   <name>java.sql.Timestamp</name>
   <expression>.*\.date_.*</expression>
   <types>.*</types>
  </forcedType>
</forcedTypes>
// [...]

DateTimeConverter class:

public class DateTimeConverter implements Converter<Timestamp, DateTime>
{

    @Override
    public DateTime from(Timestamp databaseObject)
    {
        return new DateTime(databaseObject);
    }

    @Override
    public Timestamp to(DateTime userObject)
    {
        return new Timestamp(userObject.getMillis());
    }

    @Override
    public Class<Timestamp> fromType()
    {
        return Timestamp.class;
    }

    @Override
    public Class<DateTime> toType()
    {
        return DateTime.class;
    }
}

So, BOOK.DATE_PUBLISHED is being generated like this:

public final org.jooq.TableField<com.plannow.jooq.db.tables.records.BookRecord, java.sql.Timestamp> DATE_PUBLISHED = createField("date_published", org.jooq.impl.SQLDataType.TIMESTAMP, this, "", new com.plannow.jooq.converters.DateTimeConverter());

which leads to compilation error:

Type mismatch: cannot convert from TableField<BookRecord,DateTime> to TableField<BookRecord,Timestamp>.

I know i can change type of DATE_PUBLISHED to TableField<BookRecord,DateTime> and refactor the code, but i dont want to patch the generated classes manually.

Any ideas what am i doing wrong?

The <name/> element of your <customType/> should refer to the <U> type (user type) of your Converter<T, U> , not to the <T> type (database type). So if you write this:

<customTypes>
  <customType>
   <name>java.sql.Timestamp</name>
   <converter>com.plannow.jooq.converters.DateTimeConverter</converter>
  </customType>         
</customTypes>

Then you're really just registering a Converter<Timestamp, Timestamp> . Try this instead:

<customTypes>
  <customType>
   <name>org.joda.time.DateTime</name>
   <converter>com.plannow.jooq.converters.DateTimeConverter</converter>
  </customType>         
</customTypes>

Note that your converter should also correctly handle null values:

@Override
public DateTime from(Timestamp t)     {
    return t == null ? null : new DateTime(t);
}

@Override
public Timestamp to(DateTime u) {
    return u == null ? null : new Timestamp(u.getMillis());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM