如何使用Hibernate Envers检索关系@ManyToOne的经过审计的注册表 [英] How to retrieve audited register with relationship @ManyToOne using Hibernate Envers
问题描述
我很怀疑使用 Hibernate Envers
和我的类。
我有一个类 Loja
。
@Entity
@Audited
@GenericGenerator (name =Sequence_Generic,strategy =com.paradigma.ecred.dao.hibernate.generator.ManualGenerator)//序列泛型criado para atividade 510
@SelectBeforeUpdate @DynamicUpdate
public class Loja extends持久性{
@Trim
@NotBlank(message =O preenchimento do campo \CNPJ \éobrigatório。)
@CNPJ(message =O \\ \\CNPJ da loja \éinválido)
private String cnpj;
@Trim
@NotBlank(message =O preenchimento do campo \Razãosocial \éobrigatório。)
@Size(max = 255,message = aRazãosocial deve conter nomáximo{max} caracteres。)
private String razaoSocial;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name =idlojamaster,referencedColumnName =id,columnDefinition =integer)
private Loja lojaMaster;
@ManyToOne
@JoinColumn(name =idseguradora,referencedColumnName =id,columnDefinition =integer)
private Seguradora seguradora;
@ManyToOne
@JoinColumn(name =idTabelaSeguro,referencedColumnName =id,columnDefinition =integer)
private TabelaSeguro tabelaSeguro;
// getter e setter
}
我想知道审计字段 lojaMaster
, seguradora
, tabelaSeguro
。这些类用 @Audited
标记。当我进行像插入
或编辑
的操作时, Id
值存储在 loja_aud
表中。但是,当我回顾这些值以鞋形式运行时,当在 Eclipse中调试时收到此消息
com.sun.jdi.InvocationException
。
$ b 它通过Hibernate执行sql,但类仍然为空,我还在这些类中找到了一个方法, handler
code>。它包含我的对象的 id
。
我试图找到信息,但它非常困难。
所以有人可以帮助我!
处理我的问题,可以是你的。
我调试了我的代码,并在这些实体中找到 lojaMaster
, seguradora
和 tabelaSeguro
一个名为handler的方法。我的问题是,该系统是在一个较旧的数据库中开发的。因此 Envers
在它们的 aud表中找不到这些实体
,并且没有找到。
在这幅图中,您可以看到,处理程序是一个代理对象
,并具有
对象
。
.stack.imgur.com / RrasJ.jpgalt =Eclipse Debug>
所以我开始尝试找到一种方法来获取编号
。所以当我明白这是一个代理对象
时,我在Google中找到了解决方案 Hibernate ,而Hibernate使得这种策略成为不能获取对象所有内容的策略。
public Serializable getIdentifier(Object object){
如果(!(对象instanceof HibernateProxy)|| Hibernate.isInitialized(object)){
return((Persistent)object).getId();
}
HibernateProxy proxy =(HibernateProxy)对象;
LazyInitializer initializer = proxy.getHibernateLazyInitializer();
返回initializer.getIdentifier();
$ b 所以我得到并检查我的实体 Id
是 null
。我得到了id, findById
object
。我创建了一个 hashmap
,因为每次访问数据库
都是因为 ids
可以重复。
所以这些方法就是这样
@Override
公共列表< Pojo> getLog(Pojo pojo){
Map< Long,Loja> mapLoja = new HashMap<>();
地图< Long,Seguradora> mapSeguradora = new HashMap<>();
Map< Long,TabelaSeguro> mapTabelaSeguro = new HashMap<>();
列表< Pojo> auditedList = super.getLog(pojo);
if(!NullUtil.isNull(auditedList)){
for(Pojo pojoAudited:auditedList){
Long id = null;
if(NullUtil.isNull(pojoAudited.getLojaMaster()。getId())){
id =(Long)this.getIdentifier(pojoAudited.getLojaMaster());
this.getLojaRegister(mapLoja,id);
pojoAudited.setLojaMaster(mapLoja.get(id));
if(NullUtil.isNull(pojoAudited.getSeguradora()。getId())){
id =(Long)this.getIdentifier(pojoAudited。 getSeguradora());
this.getSeguradoraRegister(mapSeguradora,id);
pojoAudited.setSeguradora(mapSeguradora.get(id));
if(NullUtil.isNull(pojoAudited.getTabelaSeguro()。getId())){
id =(Long)this.getIdentifier(pojoAudited。 getTabelaSeguro());
this.getTabelaSeguroRegister(mapTabelaSeguro,id);
pojoAudited.setTabelaSeguro(mapTabelaSeguro.get(id));
}
}
}
return auditedList;
$ b $ private void getLojaRegister(Map< Long,Loja> mapLoja,Long id){
if(!mapLoja.containsKey(id)){
Loja loja = this.findById(id);
mapLoja.put(id,loja);
$ b $ private void getSeguradoraRegister(Map
if(!mapSeguradora.containsKey(id )){
Seguradora seguradora = this.getSeguradoraService()。findById(id);
mapSeguradora.put(id,seguradora);
private void getTabelaSeguroRegister(Map< Long,TabelaSeguro> mapTabelaSeguro,Long id){
if(!mapTabelaSeguro.containsKey(id )){
TabelaSeguro tabelaSeguro = this.getTabelaSeguroService()。findById(id);
mapTabelaSeguro.put(id,tabelaSeguro);
}
}
我希望这可以帮助有人解决 Envers
和旧数据库。
I´m doubt about using Hibernate Envers
and my class.
I have a class Loja
.
@Entity
@Audited
@GenericGenerator(name = "Sequence_Generic", strategy = "com.paradigma.ecred.dao.hibernate.generator.ManualGenerator") // sequence generic criado para a atividade 510
@SelectBeforeUpdate @DynamicUpdate
public class Loja extends Persistent {
@Trim
@NotBlank(message = "O preenchimento do campo \"CNPJ\" é obrigatório.")
@CNPJ(message = "O \"CNPJ da loja\" é inválido")
private String cnpj;
@Trim
@NotBlank(message = "O preenchimento do campo \"Razão social\" é obrigatório.")
@Size(max = 255, message = "A Razão social deve conter no máximo {max} caracteres.")
private String razaoSocial;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idlojamaster", referencedColumnName = "id", columnDefinition="integer")
private Loja lojaMaster;
@ManyToOne
@JoinColumn(name="idseguradora", referencedColumnName = "id", columnDefinition="integer")
private Seguradora seguradora;
@ManyToOne
@JoinColumn(name="idTabelaSeguro", referencedColumnName = "id", columnDefinition="integer")
private TabelaSeguro tabelaSeguro;
// getter e setter
}
I want to know to audit the fields lojaMaster
, seguradora
, tabelaSeguro
. These classes are marked with @Audited
. When I make a operation like insert
or edit
, the Id
values area stored in the loja_aud
table. But when I retrive these values to shoe in the form I got this message when debugging in Eclipse
com.sun.jdi.InvocationException
.
It executes the sql by Hibernate, but the class are still null, also I found one method inside these classes, handler
. It contains the id
of my object.
I´m trying to find informations but it´s very dificult.
So someone can help me!
解决方案 I found one way to deal with my problem that can be yours.
I was debugin my code, and found inside in these entities lojaMaster
, seguradora
and tabelaSeguro
one method called handler. My problem is, the system is been developed in a older database. So Envers
would not find these entities in their aud table
, and didn´t find.
In this picture you can see, the handler is a proxy object
and has the id
of the object
.
So I started to try to find a way to get this Id
. So when I understand this is a proxy object
I found in Google the solution Hibernate, and Hibernate make this like a strategy to no get all content of the object.
So I got the code, and got the id.
public Serializable getIdentifier(Object object) {
if (!(object instanceof HibernateProxy) || Hibernate.isInitialized(object)) {
return ((Persistent)object).getId();
}
HibernateProxy proxy = (HibernateProxy) object;
LazyInitializer initializer = proxy.getHibernateLazyInitializer();
return initializer.getIdentifier();
}
So I got and check if my entity Id
is null
. I got the id, and findById
the object
. I created one hashmap
to don´t every time access the database
because ids
can repeat.
So these methods were in this way
@Override
public List<Pojo> getLog(Pojo pojo) {
Map<Long, Loja> mapLoja = new HashMap<>();
Map<Long, Seguradora> mapSeguradora = new HashMap<>();
Map<Long, TabelaSeguro> mapTabelaSeguro = new HashMap<>();
List<Pojo> auditedList = super.getLog(pojo);
if (!NullUtil.isNull(auditedList)) {
for (Pojo pojoAudited : auditedList) {
Long id = null;
if (NullUtil.isNull(pojoAudited.getLojaMaster().getId())) {
id = (Long) this.getIdentifier(pojoAudited.getLojaMaster());
this.getLojaRegister(mapLoja, id);
pojoAudited.setLojaMaster(mapLoja.get(id));
}
if (NullUtil.isNull(pojoAudited.getSeguradora().getId())) {
id = (Long) this.getIdentifier(pojoAudited.getSeguradora());
this.getSeguradoraRegister(mapSeguradora, id);
pojoAudited.setSeguradora(mapSeguradora.get(id));
}
if (NullUtil.isNull(pojoAudited.getTabelaSeguro().getId())) {
id = (Long) this.getIdentifier(pojoAudited.getTabelaSeguro());
this.getTabelaSeguroRegister(mapTabelaSeguro, id);
pojoAudited.setTabelaSeguro(mapTabelaSeguro.get(id));
}
}
}
return auditedList;
}
private void getLojaRegister(Map<Long, Loja> mapLoja, Long id) {
if (!mapLoja.containsKey(id)) {
Loja loja = this.findById(id);
mapLoja.put(id, loja);
}
}
private void getSeguradoraRegister(Map<Long, Seguradora> mapSeguradora, Long id) {
if (!mapSeguradora.containsKey(id)) {
Seguradora seguradora = this.getSeguradoraService().findById(id);
mapSeguradora.put(id, seguradora);
}
}
private void getTabelaSeguroRegister(Map<Long, TabelaSeguro> mapTabelaSeguro, Long id) {
if (!mapTabelaSeguro.containsKey(id)) {
TabelaSeguro tabelaSeguro = this.getTabelaSeguroService().findById(id);
mapTabelaSeguro.put(id, tabelaSeguro);
}
}
I hope this can help someone with problem about Envers
and older database.
这篇关于如何使用Hibernate Envers检索关系@ManyToOne的经过审计的注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!