NH Mapping Attributes Bug (?) and Solution

15 views
Skip to first unread message

Tensfeld

unread,
Dec 22, 2010, 10:46:39 AM12/22/10
to NHibernate Contrib - Development Group
Hi,
I have the following code:

_configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Operacao)));

_configuration.AddInputStream(HbmSerializer.Default.Serialize(typeof(Usuario)));
string[] script =
_configuration.GenerateSchemaCreationScript(new
NHibernate.Dialect.MsSql2005Dialect());

And classes begins like this:
[Class(Lazy = true)]
public class Operacao
{
[Id(Name = "OperacaoId"), Generator(Class = "identity")]
virtual public int OperacaoId { get; set; }

[Class(Lazy = true)]
public class Usuario
{
[Id(Name = "UsuarioId"), Generator(Class = "identity")]
virtual public int UsuarioId { get; set; }

The GenerateSchemaCreationScript generates:
create table Operacao (OperacaoId INT not null, Operador NVARCHAR(32)
null, Descricao NVARCHAR(32) null, primary key (OperacaoId))
create table Usuario (UsuarioId INT IDENTITY NOT NULL, Login
NVARCHAR(32) null, Senha NVARCHAR(32) null, primary key (UsuarioId))

See that there's no IDENTITY in the first query. The reason is that
Visual Studio compiler is returning the atribute collection not
ordered. The line is in the HbmWriter.WriteClass() function:
object[] attributes =
type.GetCustomAttributes(typeof(ClassAttribute), false);

The solution that I've found was, to modify the base constructor call
in GeneratorAttribute.cs, instead of always define order 0, it defines
order 1, so the Id attribute will sort before Generator attribute:

GeneratorAttribute() : base(1) instead of GeneratorAttribute() :
base(0).

Of course this solution should be applied to all attribute base
constructors so the correct priority should be defined, but I am not
even sure there isn't anything strange that I am not seeing in my
VS2008.

Can someone tell me if this is right?

Pierre Henri Kuate

unread,
Dec 23, 2010, 11:41:56 AM12/23/10
to nhc...@googlegroups.com
Hi Henrique,

The usual way to solve this limitation of .NET is to provide that order as the first parameter of each attribute. So without changing NHMA, you can write:

[Id(Name = "OperacaoId"), Generator(1, Class = "identity")]

Note that I added a "1" before Class...

HTH,
Pierre Henri.



From: Tensfeld <henrique...@gmail.com>
To: NHibernate Contrib - Development Group <nhc...@googlegroups.com>
Sent: Wed, December 22, 2010 5:46:39 PM
Subject: NH Mapping Attributes Bug (?) and Solution
--
You received this message because you are subscribed to the Google Groups "NHibernate Contrib - Development Group" group.
To post to this group, send email to nhc...@googlegroups.com.
To unsubscribe from this group, send email to nhcdevs+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhcdevs?hl=en.


Tensfeld

unread,
Jan 6, 2011, 11:07:47 AM1/6/11
to NHibernate Contrib - Development Group
Hi Pierre,
yes, later I understood this mechanism. But I think this can be left
as a suggestion of improvement, since there may be an hierarchy of
attributes implicitly set in the constructor. Well, at least I think
this hierarchy exists.

Thanks!

On 23 dez 2010, 14:41, Pierre Henri Kuate <phku...@kleartouch.com>
wrote:
> Hi Henrique,
>
> The usual way to solve this limitation of .NET is to provide that order as the
> first parameter of each attribute. So without changing NHMA, you can write:
>
> [Id(Name = "OperacaoId"), Generator(1, Class = "identity")]
>
> Note that I added a "1" before Class...
>
> HTH,
> Pierre Henri.
>
> ________________________________
> From: Tensfeld <henrique.tensf...@gmail.com>
> nhcdevs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages