How to define an explicit binding with maven configuration : Coordinates - java

When i'm generating java classes from jooq generator, i get one field deprecated :
/**
* #deprecated Unknown data type. Please define an explicit {#link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using <deprecationOnUnknownTypes/> in your code generator configuration.
*/
#java.lang.Deprecated
public final TableField<PositionRecord, Object> COORDINATES = createField("coordinates", org.jooq.impl.DefaultDataType.getDefaultDataType("point"), this, "");
I am not sure to know how to define the binding on my maven configuration with the "Point" type.
Any ideas ?
Edit :
<configuration>
<!-- JDBC connection parameters -->
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/${jooq.generation.schema}</url>
<user>${jooq.generation.user}</user>
<password>${jooq.generation.password}</password>
</jdbc>
<!-- Generator parameters -->
<generator>
<generate>
<javaTimeTypes>true</javaTimeTypes>
</generate>
<database>
<name>org.jooq.util.mysql.MySQLDatabase</name>
<includes>.*</includes>
<dateAsTimestamp>true</dateAsTimestamp>
<!-- In case your database supports catalogs, e.g. SQL Server:
<inputCatalog>public</inputCatalog>
-->
<inputSchema>${jooq.generation.schema}</inputSchema>
</database>
<target>
<packageName>${jooq.generation.package}</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
So far i've been able to use my java classes generated by this configuration. But some fields are deprecated because of some "Data type" not recognized. So i've found out that we could kinda use "force type" and put it in the maven configuration + adding the corresponding binding to let know jooq about this data type. For instance in our case : we want to be able to reach the field "Coordinates" ( as a Point type in java). I hope i was clear enough.

You've already answered your question:
So i've found out that we could kinda use "force type" and put it in the maven configuration + adding the corresponding binding to let know jooq about this data type
That's the correct way, you need to use a data type binding: https://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings
Follow the instructions from the manual and you'll be fine.

Related

Generating POJOs using OpenAPI generator with Lombok Annotations

I am using OpenAPI generator maven plugin like one below for generating Java client code for models .
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
When , I generate the model classes, they get generated with usual POJO field declarations and getters and setters. But what I want to do is, instead of generating getters and setters, I want my classes to get automatically generated with Lombok annotations for Java pojos like #Getter, #Setter, #Data, etc. Is there a way to customize model generator to fit above use case requirement?
I tried to find out if there is a way. I found this discussion, where the very last comment talks about a PR, where the issue of generating models using Lombok annotations has been addressed. But I do not see any clear indication of usage or any documentation of this feature in the OpenAPI generator open source project that it has been implemented yet. So, is there any way of generating models with Lombok annotations instead of regular getters and setters today?
To complete this very old thread: Now it does support Lombok annotations.
Example taken from here
<configOptions>
<additionalModelTypeAnnotations>#lombok.Builder #lombok.NoArgsConstructor #lombok.AllArgsConstructor</additionalModelTypeAnnotations>
</configOptions>
EDIT: This answer is deprecated. See the post by #Laess3r. I'll leave this, since it is applicable for older versions of openapi generator.
openapi-generator does not yet support Lombok annotations. If you want to generate code with Lombok annotations, you need to create a custom template in mustache, as described in https://openapi-generator.tech/docs/templating/.
If you've never worked with mustache, be aware that it's somewhat hard to read, so try to keep the templates as simple as possible and make sure to add unit tests to validate the generated output. The template will look something like this:
/**
* {{#description}}{{description}}{{/description}}
*/
#Data
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}} {
{{#vars}}
/**
* {{#description}}{{description}}{{/description}}
*/
#JsonProperty("{{#lambda.lowercase}}{{nameInSnakeCase}}{{/lambda.lowercase}}")
private {{{datatypeWithEnum}}} {{name}};
{{/vars}}
I've been able to get this working out-of-the-box using a space
separated list of annotations on models:
#lombok.experimental.SuperBuilder #lombok.external.Jacksonized
If models have readOnly set to "true" the Builder becomes the only way to make the object and #Jacksonized allows it to be serialized/deserialized. There are some limitations with inheritance (turning off requiring all required parameters in the configOptions).

Persuade jOOQ not to prefix table names with the database name [duplicate]

I have 2 similar schemas in simple database - "develop" and "stage". I have generated Java classes with Jooq for one of that schemas ("develop" for example). When jooq generates query to db, it implicitly add schema's name to all query's aliases
select "develop"."image"."id", "develop"."image"."image_data"
from "develop"."image"
where "develop"."image"."id" = ?
So my question is, whether there are the way to change jooq schema name (for "stage" as an example) in generated query without regenerating jooq's classes for "stage" schema?
You have several options, which can even be combined:
Use the code generator's schema mapping feature
If you want to avoid hard-wiring the "develop" schema name into your generated classes, you can rewrite that to some other schema name like this:
<configuration>
<generator>
<database>
<schemata>
<schema>
<inputSchema>develop</inputSchema>
<outputSchema>stage</outputSchema>
</schema>
...
Of course, this just postpones the problem, because the schema name is still in the generated code. You can remove the name entirely from generated code by using the following option:
<configuration>
<generator>
<database>
<schemata>
<schema>
<inputSchema>develop</inputSchema>
<outputSchemaToDefault>true</outputSchemaToDefault>
</schema>
...
This will now remove any schema references from generated code, so the generated classes can run on all your schemas (be sure to use the correct connection and search_path, of course!)
I think this is the best option for you
More details here:
https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-catalog-and-schema-mapping/
Use the runtime schema mapping feature
You can leave generated code as it is and rewrite all object references at runtime using Settings (which you supply to your jOOQ runtime Configuration). Again, you have the same two options:
Mapping the schema name:
new Settings().withRenderMapping(new RenderMapping()
.withSchemata(new MappedSchema()
.withInput("develop")
.withOutput("stage")
)
);
Removing all schema names:
new Settings().withRenderSchema(false);
More details here:
https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping/

Inheritance with MyBatis and XML

So I got MyBatis inheritance working with annotations - child inherited #Select functionality.
But with XML files it's not working accordingly.
It will throw:
org.apache.ibatis.binding.BindingException: Invalid bound statement /.../
Saw that some used extends on the mapper element, but for me it says "Attribute extends not allowed here"
Tried <cache/> on parent and <cache-ref namespace="parent"/> on child but that threw org.apache.ibatis.builder.IncompleteElementException: No cache for namespace 'parent'
So how to get MyBatis inheritance working with XML configuration?
Attribute extends applies to resultMap only.
cache and cache-ref are about cache management.
All what may look like extending is actually factorizing: define sql fragments in a XML mapper and reference them in other mappers. E.g:
-Mapper1.xml:
<sql id="a">/* dummy will never actually been included */</sql>
<sql id="b"> something common to include </sql>
<sql id="template">
<include refid="a" />
<include refid="Mapper1.b" />
</sql>
-Mapper2.xml
<sql id="a"> something specific to this mapper </sql>
<select id="statement">
<include refid="Mapper1.template" />
</select>
include tag behaves just like copy/paste of referenced fragments.
Then the select statement will yield:
something specific to this mapper
something common to include
The trick is to play with prefixing or not referenced fragments. It may look like overriding.
I agree whit balckwizzard cache and cache-ref are not useful in this case (I have read too somewhere a post that suggest to use they to extend xml but I think that the post war an error)
I have put an answer that I think is good for you too:
Mybatis Generator: What's the best way to separate out “auto generated” and “hand edited files”
regards

How to accept key-value pairs in order from POM - Maven

I need to accept a set of key - value pairs from my POM and use them in my Mojo. I want to maintain the input order as given by the user. My POM looks as follows right now:
<plugin>
<groupId>...</groupId>
<artifactId>text-replace-plugin</artifactId>
<version>...</version>
<executions>
<execution>
...
<configuration>
<replacements>
<property>
<name>ABCD</name>
<value>XYZ</value>
</property>
<property>
<name>XYZ</name>
<value>PQR</value>
</property>
</replacements>
...
</configuration>
</execution>
</executions>
</plugin>
I am accepting the replacements inside my Mojo as Java.util's Properties which I think implements Maps and is hence not ordered.
I've tried using LinkedHashMap instead of Properties but mojo doesn't seeem to understand it.
I also tried taking in the parameters as follows, but doesn't work:
#Parameter(property = "replacer.replacements", required = true)
private List replacements;
#Parameter(property = "replacer.replacements.property")
private Map eachPair;
this gives me an error: Error loading class 'com.training.replacer.Property'
Could you suggest me a way to achieve an ordered key-value pairs input from POM.xml? Thank you!
I abandoned using Java's Properties and converted my POM's outer replacements parameter with a list variable in my mojo - List<myClass>. I achieved this by creating my own class for key,value pair and providing the source package name in POM.xml. I just need to keep the names of my data members same as the POM's property names and Maven will populate the object's data members.
Refer - https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Mapping_Complex_Objects

Dozer - Mapping to a common destination class from two source classes

Here's my scenario. I have three simple classes: SourceClassA, SourceClassB & DestClass. I have two custom converters defined inside the mapping XML in the following way:
<mappings>
<configuration>
<custom-converters>
<converter type="com.myproject.ClassAConverter">
<class-a>com.myproject.SourceClassA</class-a>
<class-b>com.myproject.DestClass</class-b>
</converter>
<converter type="com.myproject.ClassBConverter">
<class-a>com.myproject.SourceClassB</class-a>
<class-b>com.myproject.DestClass</class-b>
</converter>
...
</custom-converters>
</configuration>
</mappings>
Is this kind of mapping valid at all ? Can we map to the same destination class from two different source classes using custom converters ?
In my case, when I tried this, the ClassBConverter is never invoked at all by Dozer during bean mapping. Any thoughts ?

Categories

Resources