【发布时间】:2021-07-27 15:39:49
【问题描述】:
我正在尝试使用 jOOQ fetchInto() 方法映射到现有的 Hibernate 模型 Organization(类及其继承如下)。
Organization organization = jooq().select().from(ORGANIZATION).fetchOne().into(Organization.class);
我的问题是我无法真正理解DefaultRecordMapper 中发生了什么,因为我觉得我并不完全熟悉所使用的所有术语。我试图弄清楚它如何应用于我的代码库中的 Hibernate 类。
到目前为止我已经尝试过:
- 使用 jOOQ 生成的 POJO 来查看它是否完全检索和映射数据(有效)。
- 向
OrganizationHibernate 模型添加构造函数、getter 和setter。 - 在
OrganizationHibernate 模型中将@Column注释添加到name。
什么有效:
-
id字段已正确映射。
什么不起作用:
-
name字段未映射 (null)。 -
createdAt和modifiedAt字段未映射 (null)。
我的问题是:在映射中我是否忽略了一些东西?关于 Hibernate 模型的类、字段、构造函数和注释,我应该注意什么?我想最终映射代码库中的所有 Hibernate 模型并使用 fetchInto 来做到这一点。
谢谢! :)
@Entity
public class Organization extends BaseModel {
@Required public String name;
//... a lot of other code
}
@MappedSuperclass
public class BaseModel extends Model {
/** The datetime this entity was first saved. Automatically set by a JPA prePersist */
@NoBinding
@Column
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime createdAt;
/** The datetime this entity was last modified. Automatically set by a JPA preUpdate */
@NoBinding
@Column
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
public DateTime modifiedAt;
//...
}
@MappedSuperclass
public class Model extends GenericModel { // Both Model and GenericModel are from the Play Framework
@Id
@GeneratedValue
public Long id;
public Model() {
}
public Long getId() {
return this.id;
}
public Object _key() {
return this.getId();
}
}
【问题讨论】:
-
为什么需要映射到 Hibernate 实体?向后兼容?更快的迁移路径?还是因为您想通过实体管理器再次持久化更改?
-
所以基本上向后兼容模型中存在的大量方法。因此,您对链接的初步回答可能是我目前解决此问题的最佳选择。
-
有道理。我假设这些方法包含业务逻辑。我认为这也不是 JPA 所推荐的,所以也许作为第一个重构步骤,您可以将它们移出实体,而与使用 jOOQ 无关?我已经更新了我的答案,并提示了
name列上的问题。 -
是的,很多业务逻辑。现在它通过将现有的字符串查询转换为 jOOQ 查询来使用 jOOQ,但让它由 Hibernate 执行。逻辑上应该遵循进一步的重构步骤。感谢大家的帮助!
标签: java hibernate playframework jooq