Pergunta

Qual é a melhor maneira de mapear o tipo uint32 para o tipo sql-server int com o Nibernate.

O valor é uma largura/altura de imagem, portanto o valor negativo não faz sentido aqui.

Mas talvez eu deva usar o INT porque o Nhibenate não suporta INTs não atribuídos.

Foi útil?

Solução

Você pode mapear a coluna com um iuserType.

<class name="UnsignedCounter">
    <property name="Count" type="mynamespace.UInt32Type, mydll"  />
</class>

E o iUserType que mapeia UInt32? e UInt32.

class UInt32Type : IUserType
{
    public object NullSafeGet( System.Data.IDataReader rs, string[] names, object owner )
    {
        int? i = (int?) NHibernateUtil.Int32.NullSafeGet( rs, names[0] );
        return (UInt32?) i;
    }

    public void NullSafeSet( System.Data.IDbCommand cmd, object value, int index )
    {
        UInt32? u = (UInt32?) value;
        int? i = (Int32?) u;
        NHibernateUtil.Int32.NullSafeSet( cmd, i, index );
    }

    public Type ReturnedType
    {
        get { return typeof(Nullable<UInt32>); }
    }

    public SqlType[] SqlTypes
    {
        get { return new SqlType[] { SqlTypeFactory.Int32 }; }
    }

    public object Assemble( object cached, object owner )
    {
        return cached;
    }

    public object DeepCopy( object value )
    {
        return value;
    }

    public object Disassemble( object value )
    {
        return value;
    }

    public int GetHashCode( object x )
    {
        return x.GetHashCode();
    }

    public bool IsMutable
    {
        get { return false; }
    }

    public object Replace( object original, object target, object owner )
    {
        return original;
    }

    public new bool Equals( object x, object y )
    {
        return x != null && x.Equals( y );
    }
}

Outras dicas

Estou um ano atrasado, mas desde que tive a mesma pergunta e encontrei uma resposta diferente, pensei em adicionar. Parece mais simples. Talvez tenha uma falha que ainda não descobri.

Estou usando o Nibernate 3.0, o Visual Studio 2005 e o .net 2.0.x.

Descobri que poderia usar a classe UINT32 da .NET e não incluir o atributo de tipo no hbm.xml.

// .NET 2.0 Property syntax
public class MyClass
{
   // NHibernate needs public virtual properties. 
   private UInt32 _Id;
   public virtual UInt32 Id { get { return (_Id); } set { _Id = value; } }
}


// hbml.xml
<class name ="MyClass">
   <id name="Id" />
</class>


// SQL to create the table
CREATE TABLE `PumpConnection` (
`Id` **INT**(10) **UNSIGNED** NOT NULL AUTO_INCREMENT,
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top