将Mapstruct用作JOOQ的RecordMapper [英] Use Mapstruct as RecordMapper for JOOQ

查看:365
本文介绍了将Mapstruct用作JOOQ的RecordMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现自己的RecordMapper并使用Mapstruct将Record映射到POJO.我不太了解如何实现这一目标.我遵循了这部分文档: https://www.jooq.org/doc/3.13/manual/sql-execution/fetching/pojos-with-recordmapper-provider/

I would like to implement my own RecordMapper and use Mapstruct to map the Record to the POJO. I don't quite understand how to accomplish this. I followed this part of the docs: https://www.jooq.org/doc/3.13/manual/sql-execution/fetching/pojos-with-recordmapper-provider/

我的映射器如下:

public class LanguageMapper<R extends Record, E> implements RecordMapper<R, Language> {

  @Override
  public Language map(R record) {
    LanguageRecord languageRecord = (LanguageRecord) record;

    // this is just an example, in the future this is the kind of mapping that would be performed automatically via mapstruct
    return new Language(
             languageRecord.getId(), 
             languageRecord.getNamespaceId(), 
             languageRecord.getLanguage(), 
             languageRecord.getCountryCode(), 
             languageRecord.getLanguageTag()
    );
  }
}

问题在于,作为record,我实际上不是我的语言表的LanguageRecord而是RecordImpl,因此无法将record转换为LanguageRecord.知道我需要更改什么吗?

The issue is that as a record I'm not actually getting a LanguageRecord but a RecordImpl of my language table and can thus not cast record to LanguageRecord. Any idea what I need to change?

使用RecordImpl时有趣的是,如果我做这样的事情

What's interesting when using the RecordImpl is, if I do something like this

record.get(LANGUAGE.LANGUAGE_TAG);

它将已经获得了错误的信息(正在获取LANGUAGE.NAMESPACE_ID).因此,当像这样获取它并将其映射到POJO时,也是错误的.

It will already get the wrong information (it's getting the LANGUAGE.NAMESPACE_ID). Thus when getting it like this and then mapping it to the POJO it will be wrong as well.

(根据此问题在JOOQ中进行POJO映射参数顺序的顺序)

推荐答案

问题是由此处描述的问题导致的:

The question was a result of the problem described here: POJO Mapping in JOOQ regardless of parameter order

但是事实证明,甚至不需要任何种类的映射器就可以解决问题.

But it turns out that the problem can be solved without even needing any sort of mapper.

问题: 如果JOOQ生成POJO/记录/等.基于数据库的数据库,数据库具有按一定顺序排列的表属性,但是在JOOQ已经生成POJO/Records/etc之后,属性的顺序会发生变化. SELECT *可能不会将字段映射到POJO中的正确属性

Problem: If JOOQ generates POJOs/Records/etc. based on a database which has the attributes of a table in a certain order, but the order of the attributes changes after JOOQ already generated the POJOs/Records/etc. it's possible that a SELECT * will not map the fields to the correct attributes in the POJOs

例如POJO Language具有属性countrylanguage,突然地Language包含Lanugage.getLanguage()中的country表值和Language.getCountry()中的lanugage表值.

E.g. The POJO Language has the attributes country and language and suddenly Language contains the table value of country in Lanugage.getLanguage() and the table value of lanugage in Language.getCountry().

可以通过在SELECT语句中指定顺序(无论数据库字段的实际顺序是什么)来解决此问题,例如SELECT language, country.

This issue can be solved by specifying an order (regardless what the actual order of the database fields are) in the SELECT-statement, like SELECT language, country.

这篇关于将Mapstruct用作JOOQ的RecordMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆