Question

I'm using Envers to audit tables, but it's creating some audit tables for unknown/absent tables. It's looks like a Many To Many relation audit table for Many To One relations.

Is this right? If it's, Why?

dbo.HorarioFixo - OK
dbo.HorarioFixo_Auditoria - OK
dbo.HorarioFixo_JanelaHorarioFixo_Auditoria - NOK
dbo.JanelaHorarioFixo - OK
dbo.JanelaHorarioFixo_Auditoria - OK

But when I try to remove/delete and HorarioFixo I'm getting an error.

The error I'm getting:

NHibernate.Exceptions.GenericADOException
could not execute batch command.[SQL: SQL not available]
   em NHibernate.Engine.ActionQueue.BeforeTransactionCompletionProcessQueue.BeforeTransactionCompletion()
   em NHibernate.Impl.SessionImpl.BeforeTransactionCompletion(ITransaction tx)
   em NHibernate.Transaction.AdoTransaction.Commit()
   em Foo.Testes.Servicos.TesteCanalDeTransmissaoService.RemoveDependenciasCorretamente() na TesteCanalDeTransmissaoService.cs: line 195
System.Data.SqlClient.SqlException
Violation of PRIMARY KEY constraint 'PK__HorarioF__450088476960C81E'. Cannot insert duplicate key in object 'dbo.HorarioFixo_JanelaHorarioFixo_Auditoria'.
Violation of PRIMARY KEY constraint 'PK__HorarioF__450088476960C81E'. Cannot insert duplicate key in object 'dbo.HorarioFixo_JanelaHorarioFixo_Auditoria'.
The statement has been terminated.
The statement has been terminated.

This is the SQL duplicated:

exec sp_executesql N'INSERT INTO HorarioFixo_JanelaHorarioFixo_Auditoria (REVTYPE, REV, HorarioFixoId, JanelaHorarioFixoId) VALUES (@p0, @p1, @p2, @p3)',N'@p0 tinyint,@p1 int,@p2 bigint,@p3 bigint',@p0=2,@p1=3,@p2=1,@p3=2 go

All this is a part of the code. If you need something more, leave a comment.

My classes:

public class Entidade
{
    protected Entidade();

    public virtual long Id { get; set; }
    public virtual long Version { get; set; }

    public abstract override bool Equals(object obj);
    public override int GetHashCode();
}

public class Horario : Entidade
{
    protected Horario()
    {

    }
}

public class HorarioFixo : Horario
{
    public virtual int Frequencia { get; set; }

    public virtual ICollection<JanelaHorarioFixo> JanelasRemessa { get; set; }

    public virtual ICollection<JanelaHorarioFixo> JanelasRetorno { get; set; }
}

public class JanelaHorarioFixo : Entidade
{
    public virtual TimeSpan HorarioInicio { get; set; }

    public virtual TimeSpan? HorarioLimite { get; set; }
}

My mappings:

public class HorarioMap : ClassMapping<Horario>
{
    public HorarioMap()
    {
        Id(x => x.Id, mapper =>
        {
            mapper.Generator(Generators.Identity);
            mapper.UnsavedValue(0);
        });
    }
}

public class HorarioFixoMap : JoinedSubclassMapping<HorarioFixo>
{
    public HorarioFixoMap()
    {
        Property(x => x.Frequencia);

        Bag(x => x.JanelasRemessa, m =>
        {
            m.Cascade(Cascade.All);
            m.Lazy(CollectionLazy.NoLazy);
        }, map => map.OneToMany());

        Bag(x => x.JanelasRetorno, m =>
        {
            m.Cascade(Cascade.All);
            m.Lazy(CollectionLazy.NoLazy);
        }, map => map.OneToMany());
    }
}

public class JanelaHorarioFixoMap : ClassMapping<JanelaHorarioFixo>
{
    public JanelaHorarioFixoMap()
    {
        Id(x => x.Id, mapper =>
        {
            mapper.Generator(Generators.Identity);
            mapper.UnsavedValue(0);
        });

        Property(x => x.HorarioInicio, m => m.NotNullable(true));

        Property(x => x.HorarioLimite, m => m.NotNullable(false));
    }
}

NH and Envers configurations:

var ormHelper = ORMHelperUtils.GetORMHelper();

var mapper = new MyConventionModelMapper();

_config = new Configuration();

mapper.AddMappings(ormHelper.GetMappings());
mapper.AddMapping(typeof(REVINFOMap));
ormHelper.SetupApplicationNeeds(_config);

_config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
_config.SetProperty(Environment.CurrentSessionContextClass, "call");

if (ormHelper.UseEnvers)
{
    var classesDominio = ormHelper.GetDomainTables();

    if (classesDominio.Any())
    {
        var envers = new FluentConfiguration();
        envers.Audit(classesDominio);

        envers.SetRevisionEntity<REVINFO>(e => e.Id, e => e.Date, new CustomRevisionListener());

        _config.SetEnversProperty(ConfigurationKey.AuditTableSuffix, "_Auditoria");
        _config.IntegrateWithEnvers(envers);
    }
}
Was it helpful?

Solution 2

If you use unidirectional one-to-many, Envers needs a link table to be able to have correct history.

If you use bidirectional one-to-many, no link table is needed.

See this answer.

OTHER TIPS

I've just changed my class to

public class HorarioFixo : Horario
{
    public virtual int Frequencia { get; set; }

    public virtual ICollection<JanelaHorarioFixo> Janelas { get; set; }
}

And added a property to JanelaHorarioFixo to identify the type. But the table dbo.HorarioFixo_JanelaHorarioFixo_Auditoria is still there, and I don't know why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top