How to define a CHAR (ORACLE) column in Nhibernate?

I am starting to work with Nhibernate and Oracle for a project.
The database is Oracle 9.2, and I cannot change the schema or anything else.
I am using NH3.0 and Oracle.DataAccess.dll ver 2.111.7.20.

So far I have mapped a couple of tables and made some queries. Everything works very well. I have run into a problem now and I don’t know how to fix it. The company that developed the database thought it would be nice to create all the alphanumeric fields like CHAR instead of VARCHAR or VARCHAR2.

I matched all of these columns as String, and my classes have string fields. Later I tried to load an object by its primary key, defined as CHAR (10) in Oracle. The key I tried to download is just 7

EG characters : 'CI00252' Apparently, my entity could not be loaded. Query profiling with NHProf. I see that my query is fine, and if I try to execute it in Oracle Sql-Developer, I get a result set. I can get it to work if I put a line like this: CI00252.

Given that most of the fields defined in the database are CHARs, it is impossible for me to fill out everything before executing the query.
What can I do to fix this problem?

PS: I saw some other people with the same problem, but I did not find a suitable answer.

UPDATE

I read a blog and this guy had a similar problem with a different data type. I tried adpapt code

public SqlType[] SqlTypes
    {
        get
        {
                SqlType[] types = new SqlType[1];
                types[0] = new SqlType(DbType.StringFixedLength);
                return types;
                }
    }

**

and apparently everything works, but ... I don’t know why.

using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

namespace ConsoleOracleNhibernate.OracleTypes
{
    public class CharUserType : IUserType
    {
        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            string resultString = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (resultString != null)
                return resultString.Trim();
            return null;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                NHibernateUtil.String.NullSafeSet(cmd, null, index);
                return;
            }

            value = ((String)value).Trim();

            NHibernateUtil.String.NullSafeSet(cmd, value, index);
        }

        public object DeepCopy(object value)
        {
            if (value == null) return null;
            return string.Copy((String)value);
        }

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

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

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

        public SqlType[] SqlTypes
        {
            get
            {
                SqlType[] types = new SqlType[1];
                types[0] = new SqlType(DbType.StringFixedLength);
                return types;
            }
        }

        public Type ReturnedType
        {
            get { return typeof(String); }
        }

        public bool IsMutable
        {
            get { return false; }
        }

        public new bool Equals(object x, object y)
        {
            if (x == null || y == null) return false;
            return x.Equals(y);
        }

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

and my mapping:

<key-property name="CustomerCode" column="ANCOCO" type="ConsoleOracleNhibernate.OracleTypes.CharUserType, ConsoleOracleNhibernate" length="10"></key-property>

Is there anyone here who can help me understand what is happening?

+3
source share
1 answer

My answer is in updating the question.

+1
source

Source: https://habr.com/ru/post/1795543/


All Articles