Hibernate 映射到具有JPA注释的实体的旧版本
我最近发现了enversforauditing,并成功地使用它跟踪修订,并使用Hibernate 映射到具有JPA注释的实体的旧版本,hibernate,jpa,hibernate-envers,Hibernate,Jpa,Hibernate Envers,我最近发现了enversforauditing,并成功地使用它跟踪修订,并使用@Audited注释和AuditReader获取修订。现在,我试图实现的是在进行修订时保留到审计实体的映射,而不是最新修订 快速示例: 假设我有一个cookie的配方,我用它来批量制作cookie(下面的类是伪类)。每个配方都有一个要遵循的说明列表,这样做会创建一个批次: @Audited @Table(name="recipes") class CookieRecipe { @OneToMany(mapped
@Audited
注释和AuditReader
获取修订。现在,我试图实现的是在进行修订时保留到审计实体的映射,而不是最新修订
快速示例:
假设我有一个cookie的配方,我用它来批量制作cookie(下面的类是伪类)。每个配方都有一个要遵循的说明列表,这样做会创建一个批次:
@Audited
@Table(name="recipes")
class CookieRecipe {
@OneToMany(mappedBy="recipe")
private List<RecipeStep> steps;
private void addStep(String instruction) {
steps.add(new RecipeStep(instruction));
}
}
@Table(name="batches")
class CookieBatch {
@ManyToOne
@JoinColumn(...)
private CookieRecipe recipe;
}
@Audited
@Table(name="recipe_step")
class RecipeStep {
@Column
private String instruction;
@ManyToOne
@JoinColumn(...)
private CookieRecipe recipe;
private RecipeStep(String instruction) {
this.instruction = instruction;
}
}
我将用这个食谱来制作我的第一批饼干:
CookieBatch batch = new CookieBatch(recipe);
entityManager.persist(batch);
例如,如果我想将配方改为375F而不是400F,这将创建CookieRecipe
的第2版,这正是我所期望和想要的。但是,我希望我已经创建的批处理指向CookieRecipe
的修订版1。目前,如果我获取我已经使用ID创建的CookieBatch
,则对CookieRecipe
的引用将成为最新版本(带有375F的版本)
这是我使用envers可以完成的吗?我建议对
CookieBatch
进行审核,并在CookieBatch
和CookieCipe
之间保持双向关系。通过这种方式,Envers可以从任意一方正确地查询适当的版本
换句话说,将以下内容添加到CookieRecipe
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
private List<CookieRecipeBatch> batches = new ArrayList<>();
@OneToMany(mappedBy=“recipe”,cascade=CascadeType.ALL)
私有列表批次=新的ArrayList();
然后,可以使用以下循环获取相应的版本数据:
AuditReader reader = AuditReaderFactory.get( session );
for ( Number revision : reader.getRevisions(CookieRecipe.class, recipeId ) ) {
CookieRecipe recipe = reader.find( CookieRecipe.class, recipeId, revision );
// recipe.getSteps() - contains all steps with revision number <= revision
// recipe.getBatches() - contains all batches with revision number <= revision
}
AuditReader=AuditReaderFactory.get(会话);
for(编号修订:reader.getRevisions(CookieRecipe.class,recipeId)){
CookieRecipe recipe=reader.find(CookieRecipe.class,recipeId,revision);
//recipe.getSteps()-包含修订号为的所有步骤。我建议审核CookieBatch
,并在CookieBatch
和CookieCiepe
之间保持双向关系。这样,Envers可以从任何一方正确查询相应的版本
换句话说,将以下内容添加到CookieRecipe
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
private List<CookieRecipeBatch> batches = new ArrayList<>();
@OneToMany(mappedBy=“recipe”,cascade=CascadeType.ALL)
私有列表批次=新的ArrayList();
然后,可以使用以下循环获取相应的版本数据:
AuditReader reader = AuditReaderFactory.get( session );
for ( Number revision : reader.getRevisions(CookieRecipe.class, recipeId ) ) {
CookieRecipe recipe = reader.find( CookieRecipe.class, recipeId, revision );
// recipe.getSteps() - contains all steps with revision number <= revision
// recipe.getBatches() - contains all batches with revision number <= revision
}
AuditReader=AuditReaderFactory.get(会话);
for(编号修订:reader.getRevisions(CookieRecipe.class,recipeId)){
CookieRecipe recipe=reader.find(CookieRecipe.class,recipeId,revision);
//recipe.getSteps()-包含所有修订号为的步骤,我相信您唯一的方法是在CookieBatch
中保留recipeId
,RecipeVisionNumber
字段,并自己加载CookieRecipe
对象
@Table(name="batches")
class CookieBatch {
@Column(...)
Long recipeId;
@Column(...)
Long recipeRevisionNumber;
@Transient
private CookieRecipe recipe;
@PostLoad
public void loadRecipe()
{
// Load a cookie recipe via audit criteria
}
}
审计标准是不言自明的,请查看以下示例:
和所有东西的文档envers:
我相信您唯一的方法是在CookieBatch
中保留recipeId
,RecipeVisionNumber
字段,并自己加载CookieRecipe
对象
@Table(name="batches")
class CookieBatch {
@Column(...)
Long recipeId;
@Column(...)
Long recipeRevisionNumber;
@Transient
private CookieRecipe recipe;
@PostLoad
public void loadRecipe()
{
// Load a cookie recipe via audit criteria
}
}
审计标准是不言自明的,请查看以下示例:
和所有东西的文档envers:
CookieBatch
没有被审计是故意的吗?如果是这样,我不相信有一种优雅的方式来做你想做的事情。@SergeiBednar是的,唯一的原因是一旦CookieBatch
完成了,它就不会改变。所以我认为这是不必要的,因为它只会创建一个审计表,这样就可以了永远不要真正被使用…除非我误解了这种行为。我不反对将@Audited
添加到它中,如果它导致了解决方案。我认为这里唯一的解决方案是保留recipeId
和RecipeVisionNumber
值,将CookieBatch
中的配方
实体更改为@Tr加载CookieBatch
实体后,ansient
,并通过enversAuditCriteria
,自行处理配方查询。@SergeiBednar我认为这将是适合我的解决方案,介意将其作为解决方案发布吗?此外,您是否了解如何在p保存CookieBatch
?CookieBatch
应该保存到这一点,您可以通过审核标准获得最新版本。查看此文档,它非常可靠:CookieBatch
未被@Audited
是故意的?如果是这样,我不相信有一种优雅的方式来完成您的任务“我们正试图这么做。@SergeiBednar是的,唯一的原因是一旦CookieBatch
完成,它就不会改变。所以我认为这是不必要的,因为它只会创建一个永远不会被真正使用的审核表……除非我误解了这种行为。如果能够找到解决方案,我不反对向它添加@Audited
。我认为您唯一的其他解决方案是保留recipeId
和recipeRevisionNumber
值,将CookieBatch
中的配方
实体更改为@Transient
,并在加载CookieBatch后,通过envers审核标准
自行处理配方查询e> 实体。@SergeiBednar我认为这将是适合我的解决方案,介意将其作为解决方案发布吗?此外,您是否了解如何在保存CookieBatch
时获取recipeRevisionNumber
?此时应保存CookieRecipe
,您可以通过审计准则