英文:
JOOQ + JPA entities
问题
以下是翻译好的内容:
我对JOOQ库还不熟悉,但有一件事情让我很感兴趣。起初,我在JOOQ上实现了CRUD服务,然后我尝试避免一些重复的代码。为了达到这个目标,我添加了JPA存储库,并在由JOOQ生成的类上添加了@Entity
注释。现在,我仍然希用于某些情况下继续使用JOOQ(使用筛选、排序和分页查询列表)。但是出现了一些问题,现在在JOOQ进行选择查询后,我可以在类的属性中看到空值。
通过筛选,我可以得到正确数量的实体,但在映射后,类的属性为空。是映射有问题,还是我无法在这种情况下同时使用JOOQ和JPA?
我所有实体的抽象类(正如我所说,为了避免重复代码,我已经重构了一些代码,现在使用泛型):
@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;
}
}
我的JPA类(由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;
// ...
}
从数据库中提取实体的方法:
@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);
}
}
英文:
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
得分: 1
因为我的搜索结果,我在 Account 类的注解方面犯了一个错误。如果你想要将这些框架一起使用,你应该在实体的属性上使用 @Column,或者以不同的方式配置你的 jooq 代码生成插件)
这个资源对我很有帮助
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论