是什么让jOOQ停止在我的POJO上映射列

时间:2017-11-21 10:33:47

标签: java postgresql jooq

我已成功使用jOOQ几个月了,使用@Column注释在我的POJO上为jOOQ提供列映射。

然后,在上周代码合并之后,一些映射停止了工作。很显然,我已经经历了很多Diffs,试图找到代码中发生了哪些变化,导致行为发生变化,但无济于事。

例如,我有下表:

CREATE TABLE public.address
    (
        address_id integer NOT NULL DEFAULT nextval('address_address_id_seq'::regclass),
        address_line_1 character varying(255) NOT NULL,
        address_line_2 character varying(255),
        address_line_3 character varying(255),
        town_city character varying(255),
        postcode character varying(20),
        country_id integer NOT NULL,
        CONSTRAINT address_pkey PRIMARY KEY (address_id),
        CONSTRAINT fk_address_country_id FOREIGN KEY (country_id)
            REFERENCES public.country (country_id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
    )

和这个带注释的POJO:

@Column(name="ADDRESS_ID")
private String addressId;
@Column(name="ADDRESS_LINE_1")
private String addressLine_1;
@Column(name="ADDRESS_LINE_2")
private String addressLine_2;
@Column(name="ADDRESS_LINE_3")
private String addressLine_3;
@Column(name="TOWN_CITY")
private String townCity;
@Column(name="POSTCODE")
private String postcode;
@Column(name="COUNTRY_ID")
private String countryId;

public String getAddressLine_1() {
    return addressLine_1;
}

public void setAddressLine_1(String addressLine_1) {
    this.addressLine_1 = addressLine_1;
}

public String getAddressLine_2() {
    return addressLine_2;
}

public void setAddressLine_2(String addressLine_2) {
    this.addressLine_2 = addressLine_2;
}

public String getAddressLine_3() {
    return addressLine_3;
}

public void setAddressLine_3(String addressLine_3) {
    this.addressLine_3 = addressLine_3;
}

public String getTownCity() {
    return townCity;
}

public void setTownCity(String townCity) {
    this.townCity = townCity;
}

public String getPostcode() {
    return postcode;
}

public void setPostcode(String postcode) {
    this.postcode = postcode;
}

public String getAddressId() {
    return addressId;
}

public void setAddressId(String addressId) {
    this.addressId = addressId;
}

public String getCountryId() {
    return countryId;
}

public void setCountryId(String countryId) {
    this.countryId = countryId;
}

要使jOOQ将数据库中的值映射到POJO,我必须将POJO上的地址字段从adressLine1,addressLine2和addressLine3重命名为addressLine_1,addressLine_2和addressLine_3。

要明确的是,他们在字段和访问者名称之前没有下划线工作"合并"。 我还发现,通过研究已经出现同样问题的其他类,访问器函数的命名无关紧要。只是字段的名称。

所以我想知道是否有一些配置会导致行为发生这种变化,或者我的映射可能会出现一些错误?

1 个答案:

答案 0 :(得分:0)

似乎在列名的末尾有下划​​线和数字(例如_1,_2),字段名称将包含下划线。要解决此问题,请编辑您的pom.xml:

<generator>
    ...
    <strategy>
        <matchers>
            <fields>
                <field>
                    <expression>^(.*?)_(.\d*)$</expression>
                    <fieldGetter>
                        <transform>CAMEL</transform>
                        <expression>get_$1$2</expression>
                    </fieldGetter>
                    <fieldSetter>
                        <transform>CAMEL</transform>
                        <expression>set_$1$2</expression>
                    </fieldSetter>
                    <fieldMember>
                        <transform>CAMEL</transform>
                        <expression>$1$2</expression>
                    </fieldMember>
                </field>
            </fields>
        </matchers>
    </strategy>
</generator>

第一个表达式标记包含将要考虑的字段名称:以下划线和数字结尾的字段名称。其他表达式标记将构造一个由前缀(对于getter get_,对于setter set_,对于字段成员无)和下划线两侧的字符串($ 1和2 $)组成的字符串。后来的字符串将转换为驼峰式大小写,从而删除所有下划线并将其视​​为单词分隔符。

例如,您的address_line_1设置器将如下所示: 应用表达式后设置为set_address_line1,转换后设置为setAddressLine1。