Envers hibernate problem with class objects within a class

Asked

Viewed 461 times

-1

I have the following 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
}

Along with these three fields, lojaMaster, seguradora, tabelaSeguro. That are related to yourself and other tables. These other tables have their audit tables and their classes are marked with @audited.

But when I debug the code and click on one of these attributes, it presents the following message in Eclipse.

com.sun.Jdi.Invocationexception

And they stay null. Strange that when I perform some operation in this table it records in the table aud, the records normally, as the ids of these tables.

Am I doing something wrong? How should I proceed to be able to perform the audit of this table?

  • Just so I can better understand what you are asking. Does your application provide users with data from the audit tables? For example: pessoa_aud, endereco_aud, revtype. And do you allow changes to these tables by users? What I don’t understand is the relationship you are drawing between the audit tables with the "normal" tables. There is no business relationship there, as the name says, it is only and only to audit (you know the modifications in the normal tables). And each audit table is independent of each other.

  • @Ricardogiaviti ,look I tried to quote an example out of what I am working on to be simpler. I will modify the description of the question text.

1 answer

0


I got what you need from the following fact, the database Envers this acting already has older records. So when the Envers checked he did not find a certain object because he did not find it in the audit.

In performing debug in the code I checked that within the attributes of the Shop class, as lojaMaster, seguradora and tabelaSeguro below you can check in the following figure, inside one of these objects I found an attribute or method foreign to it, which would be calling handler. I started researching and verified that this method has to do with the fact that this object is proxy for its relationship, because the Hibernate only brings the data when required, fact that when occurred, as already said above, did not happen correctly because it did not find the record in the table.

But seeing the contents of handler, I saw that it had an attribute id, was just what I needed, so I could make an alternative solution that in case would effect the search of the register in the bank.

Anyway, I started searching for the proxy id. I found the forum solution that this method would be.

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();
}

He returns me the id of the object.

So I look for the id, but not to keep accessing the bank at all times I proposed to make a code where I fill a map with the id and the object to be located.

Below this the method.

@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);
    }
}

Anyway, I’ve achieved my goal. I hope this can help people with the same problem as me. Thank you

Eclipse degub em ação

Browser other questions tagged

You are not signed in. Login or sign up in order to post.