I’ve already answered it on SO. At least the “why and if a link table is 
needed”. If I got the SO message and his own answer correctly, the exception 
problem has been solved (?).

From: [email protected] [mailto:[email protected]] On Behalf Of 
José Henrique
Sent: den 26 mars 2014 14:11
To: [email protected]
Subject: [nhusers] NHibernate Envers: Cannot insert duplicate key in object

A friend who works with me have this problem and posted it on 
stackoverflow<http://stackoverflow.com/q/22661873/20683>.
I take and post here also, if anyone knows assist us, we thank you.

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);
        }
    }
--
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to