java - How to retrieve audited register with relationship @ManyToOne using Hibernate Envers -


i´m doubt using hibernate envers , class.

i have class loja.

@entity @audited @genericgenerator(name = "sequence_generic", strategy = "com.paradigma.ecred.dao.hibernate.generator.manualgenerator") // sequence generic criado para atividade 510 @selectbeforeupdate @dynamicupdate public class loja extends persistent {      @trim     @notblank(message = "o preenchimento campo \"cnpj\" é obrigatório.")      @cnpj(message = "o \"cnpj da loja\" é inválido")     private string cnpj;      @trim     @notblank(message = "o preenchimento 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 know audit fields lojamaster, seguradora, tabelaseguro. these classes marked @audited. when make operation insert or edit, id values area stored in loja_aud table. when retrive these values shoe in form got message when debugging in eclipse com.sun.jdi.invocationexception.

it executes sql hibernate, class still null, found 1 method inside these classes, handler. contains id of object.

i´m trying find informations it´s dificult.

so can me!

i found 1 way deal problem can yours.

i debugin code, , found inside in these entities lojamaster, seguradora , tabelaseguro 1 method called handler. problem is, system been developed in older database. envers not find these entities in aud table, , didn´t find.

in picture can see, handler proxy object , has id of object.

eclipse debug

so started try find way id. when understand proxy object found in google solution hibernate, , hibernate make strategy no content of object.

so got code, , got 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 got , check if entity id null. got id, , findbyid object. created 1 hashmap don´t every time access database because ids can repeat.

so these methods in 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)) {          (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 can problem envers , older database.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -