Search code examples
javaspringjooq

JPA-annotated POJOs are not mapped correctly when @Column is not used for all members


I have a custom POJO on which I am mapping the database records using JOOQ .fetchInto(TestClassDto.class). Most of the fields in my POJO are exactly similar to a database table's columns. However, there are a few that are different, therefore, I added java persistence and used @Column to explicitly map such columns on my POJO as described here.

Unfortunately, this is not working if I use @Column on a few specific fields. Only the fields that are annotated with @Column are mapped and the rest are ignored and set Null even though they are similar to the table column name and should be mapped implicitly.

Could you give me a hint if I am missing anything?

Sample POJO:

@Getter
@Setter
public class TestClassDto {

    @Column(name = "field_AB_XYZ") // explicit mapping is required, thus need @Column
    private Long myfieldAB;

    /* Here, mapping could be implicitly done without using @Column because 
    ** database column name and POJO property is same but it stays empty if I 
    ** ignore @Column */
    @Column(name = "hello_world") 
    private Long helloWorld;

}

Lastly, If I completely remove @Column from POJO's properties, helloWorld property is filled (implicitly) but myfieldAb remains NULL (because mapping is not found as expected).

Below is sample query:

dslContext.select()
      .from(SOMETHING)
      .where(SOMETHING.NAME.eq("Something"))
      .fetchInto(TestClassDto.class)

Solution

  • As of jOOQ 3.15, you either have to annotate

    • all of your attributes...
    • none of your attributes...

    ... with the @Column annotation. There's a pending feature request to mimick JPA more closely and make the @Column annotation optional for some attributes: https://github.com/jOOQ/jOOQ/issues/4586.

    In the meantime, instead of using those JPA annotations, you could add auxiliary getters/setters for your column:

    public void setFieldAbXyz(Long v) {
        this.myfieldAB = v;
    }
    
    public Long getFieldAbXyz() {
        return myfieldAB;
    }