JOOQ + JPA entities

Viewed 1079

I'm new with JOOQ library and have one thing interesting me so much. I've implemented CRUD service on JOOQ at first and after that I've tried to avoid some duplicate code. For reach that goal I've added JPA repository and also added@Entity annotation to my generated by JOOQ class. And now I still want to use JOOQ for some cases (querying List using filter and sorting and pagination). But something went wrong and now after JOOQ makes a select request I can see nulls in my class's attributes.

I'm getting right count of entities by filter, but class's properties are null after mapping. Is that mapping wrong or I just could't use JOOQ and JPA together for this case?

My abstact class for all entities (as I said, for avoid duplicating code I've refactored some code and now use generics):

@MappedSuperclass
public abstract class AbstractServiceEntity {

private Integer id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
}

My JPA class (generated by JOOQ):

/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
@Entity
@Table(schema = "ref", name = "account")
public class Account extends AbstractServiceEntity implements Serializable {

   private static final long serialVersionUID = -162537472;

private Integer       id;
private Integer       transitId;
private Integer       partnerId;
private String        currencyCode;
private String        descr;
private Long          inCredit;
private Long          balanceLimit;
private Long          outCredit;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer       transitPartnerId;

public Account() {}

public Account(Account value) {
    this.id = value.id;
    this.transitId = value.transitId;
    this.partnerId = value.partnerId;
    this.currencyCode = value.currencyCode;
    this.descr = value.descr;
    this.inCredit = value.inCredit;
    this.balanceLimit = value.balanceLimit;
    this.outCredit = value.outCredit;
    this.createdAt = value.createdAt;
    this.updatedAt = value.updatedAt;
    this.transitPartnerId = value.transitPartnerId;
}

public Account(
    Integer       id,
    Integer       transitId,
    Integer       partnerId,
    String        currencyCode,
    String        descr,
    Long          inCredit,
    Long          balanceLimit,
    Long          outCredit,
    LocalDateTime createdAt,
    LocalDateTime updatedAt,
    Integer       transitPartnerId
) {
    this.id = id;
    this.transitId = transitId;
    this.partnerId = partnerId;
    this.currencyCode = currencyCode;
    this.descr = descr;
    this.inCredit = inCredit;
    this.balanceLimit = balanceLimit;
    this.outCredit = outCredit;
    this.createdAt = createdAt;
    this.updatedAt = updatedAt;
    this.transitPartnerId = transitPartnerId;
}

And my method extracting entities from DB:

@Repository
@RequiredArgsConstructor
public class JooqAccountRepository {

private final DSLContext jooq;

public List<Account> findAll(Condition filterCondition, SortField[] sortFields, Integer partnerId, Integer limit, Integer offset) {
    return jooq.selectFrom(ACCOUNT)
            .where(ACCOUNT.PARTNER_ID.equal(partnerId))
            .and(filterCondition)
            .orderBy(sortFields)
            .limit(limit)
            .offset(offset)
            .fetchInto(Account.class);
}

public Integer findAccountsCount(Integer partnerId) {
    return jooq.selectCount().from(ACCOUNT)
            .where(ACCOUNT.PARTNER_ID.equal(partnerId))
            .fetchOne(0, Integer.class);
}

}

1 Answers

As a result of my searches - I've made a mistake with annotations in Account class. If you want use these frameworks together, you should use @Column on entity's properties or setting your jooq's codegen plugin in different way) This resource was usefull for me

Related