maven plugin, generate kotlin dataclasses - howto?

141 views
Skip to first unread message

David Karlsen

unread,
Oct 21, 2020, 8:28:18 AM10/21/20
to jooq...@googlegroups.com
I have a configuration section like:
<generate>
<javaTimeTypes>true</javaTimeTypes>
<jpaAnnotations>true</jpaAnnotations>
<jpaVersion>2.2</jpaVersion>
<pojosAsKotlinDataClasses>true</pojosAsKotlinDataClasses>
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
<pojos>true</pojos>
<daos>true</daos>
<validationAnnotations>false</validationAnnotations>
<springAnnotations>true</springAnnotations>
</generate>
but still the classes generated are .java and not Kotlin.

--

Lukas Eder

unread,
Oct 21, 2020, 8:49:04 AM10/21/20
to jOOQ User Group
Hi David,

This flag currently requires using the KotlinGenerator. I guess it would make sense to enable it also in the JavaGenerator? Was that your expectation? Is there a reason why you'd like Kotlin data classes, but all the other generated classes as ordinary Java classes?

Thanks,
Lukas

--
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 jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAGO7Ob3zK2Nhqo0WCMMfLyMoqAEGzy2E7Cw6G-6XizkObXfYdQ%40mail.gmail.com.

David Karlsen

unread,
Oct 21, 2020, 9:18:59 AM10/21/20
to jooq...@googlegroups.com
Isn't the same generator used under the hood from the maven plugin? I want Kotlin code since my app is kotlin (except for generated jooq and openapi code). If it is possible to get all code generated as Kotlin I'd prefer that.
Thanks.

Lukas Eder

unread,
Oct 21, 2020, 10:45:51 AM10/21/20
to jOOQ User Group
The code is the same, yes. But in order to activate it, you have to use:

<configuration>
  <generator>
    <name>org.jooq.codegen.KotlinGenerator</name>
  </generator>
</configuration>

This will be documented soon.

David Karlsen

unread,
Oct 21, 2020, 11:42:23 AM10/21/20
to jooq...@googlegroups.com
Great - now I get kotlin code!
But I think the generator could be tightened a bit:

create table T_CUSTOMER (
id number(19,0) not null, -- generated PK

record:
var id: BigInteger?
set(value) = set(0, value)
@Id
@Column(name = "ID", nullable = false, precision = 19)
get() = get(0) as BigInteger?
if it is not nullable - then it can be BigInteger - not BigInteger?

same with pojo:
data class TCustomer(
var id: BigInteger? = null
, var version: BigInteger? = null
, var customerid: String? = null
, var orgid: String? = null
, var firstname: String? = null
, var lastname: String? = null
, var createdDt: LocalDateTime? = null
, var updatedDt: LocalDateTime? = null
): Serializable {

wdyt?

Lukas Eder

unread,
Oct 21, 2020, 12:03:22 PM10/21/20
to jOOQ User Group
Hi David,

But it *is* nullable. In SQL, everything is nullable. Rationale here:

One simple example would be using a LEFT JOIN:

SELECT c.*
FROM x
LEFT JOIN t_customer c
ON false

If you run this query with jOOQ, pretending that your columns are NOT NULL would be a mistake. The same is true with unions, expressions, and many other SQL features, and even with deferred constraints or triggers, which we are currently unaware of.

There might be a flag in the future that allows you to override this at your own risk:

In addition to that, you may choose to project only 2 columns but still map your record into the POJO that has been generated. Another source of nulls in jOOQ...

Are those ill-formatted JPA annotations a copy paste artifact, or generated that way?

I hope this helps,
Lukas

David Karlsen

unread,
Oct 21, 2020, 12:14:55 PM10/21/20
to jooq...@googlegroups.com
Ok — that is reasonable.
The annotations were generated like that

Lukas Eder

unread,
Oct 22, 2020, 5:12:08 AM10/22/20
to jOOQ User Group
OK, thanks for the confirmation. I can see a warning in the logs, when turning on JPA annotations:

11:09:05,377 [INFO] --- jooq-codegen-maven:3.15.0-SNAPSHOT:generate (generate-daos) @ jooq-test-codegen-kotlin ---
java.lang.IllegalStateException: A formatting error has been produced by https://github.com/jOOQ/jOOQ/issues/10196
        at org.jooq.codegen.GeneratorWriter.print(GeneratorWriter.java:162)
        at org.jooq.codegen.GeneratorWriter.print(GeneratorWriter.java:143)
        at org.jooq.codegen.GeneratorWriter.println(GeneratorWriter.java:274)
        at org.jooq.codegen.JavaGenerator.printTableJPAAnnotation(JavaGenerator.java:6734)
        at org.jooq.codegen.JavaGenerator.generatePojo0(JavaGenerator.java:4155)
        at org.jooq.codegen.JavaGenerator.generatePojo(JavaGenerator.java:4124)
        at org.jooq.codegen.JavaGenerator.generatePojo(JavaGenerator.java:4105)
        at org.jooq.codegen.JavaGenerator.generatePojos(JavaGenerator.java:4092)
        at org.jooq.codegen.JavaGenerator.generate(JavaGenerator.java:595)
        at org.jooq.codegen.JavaGenerator.generate(JavaGenerator.java:538)
        at org.jooq.codegen.JavaGenerator.generate(JavaGenerator.java:437)
        at org.jooq.codegen.GenerationTool.run0(GenerationTool.java:879)
        at org.jooq.codegen.GenerationTool.run(GenerationTool.java:233)
        at org.jooq.codegen.GenerationTool.generate(GenerationTool.java:228)
        at org.jooq.codegen.maven.Plugin.execute(Plugin.java:207)

I'll investigate this here: https://github.com/jOOQ/jOOQ/issues/10782

Reply all
Reply to author
Forward
0 new messages