如何使用Hibernate Envers检索带有@ManyToOne关系的审计寄存器
我怀疑使用Hibernate Envers
和我的class级。
我有一个Loja
class。
@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 }
我想知道审计领域lojaMaster
, seguradora
, tabelaSeguro
。 这些类标有@Audited
。 当我进行类似insert
或edit
的操作时, Id
值区域存储在loja_aud
表中。 但是,当我在Eclipse
com.sun.jdi.InvocationException
调试时,我以表格的forms将这些值重新转换为shoe。
它通过Hibernate执行sql,但是类仍然是null,我在这些类中找到了一个方法, handler
。 它包含我的对象的id
。
我试图找到信息,但它非常困难。
所以有人可以帮助我!
我找到了一种方法可以解决我的问题。
我正在调试我的代码,并在这些实体里面发现了lojaMaster
, seguradora
和tabelaSeguro
这个叫做handler的方法。 我的问题是,该系统是在较旧的数据库中开发的。 所以Envers
不会在他们的aud table
找到这些实体,也没有找到。
在这张图片中你可以看到,处理程序是一个proxy object
并具有该proxy object
的id
。
所以我开始尝试找到获得此Id
。 因此,当我理解这是一个proxy object
我在Google中找到了解决方案Hibernate ,而Hibernate使这个策略变得无法获取对象的所有内容。
所以我得到了代码,并获得了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(); }
所以我得到并检查我的实体Id
是否为null
。 我得到了id,并findById
了object
。 我创建了一个hashmap
,每次都不能访问database
因为ids
可以重复。
所以这些方法都是这样的
@Override public List getLog(Pojo pojo) { Map mapLoja = new HashMap<>(); Map mapSeguradora = new HashMap<>(); Map mapTabelaSeguro = new HashMap<>(); List 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 mapLoja, Long id) { if (!mapLoja.containsKey(id)) { Loja loja = this.findById(id); mapLoja.put(id, loja); } } private void getSeguradoraRegister(Map mapSeguradora, Long id) { if (!mapSeguradora.containsKey(id)) { Seguradora seguradora = this.getSeguradoraService().findById(id); mapSeguradora.put(id, seguradora); } } private void getTabelaSeguroRegister(Map mapTabelaSeguro, Long id) { if (!mapTabelaSeguro.containsKey(id)) { TabelaSeguro tabelaSeguro = this.getTabelaSeguroService().findById(id); mapTabelaSeguro.put(id, tabelaSeguro); } }
我希望这可以帮助有关Envers
和旧数据库问题的人。
我有同样的问题,在我的情况下,我开始审计填充数据库。 当我使用空数据库时,一切正常。 我从中获取信息