转换“t”时出现流利的 nhibernate 错误; 'f' to boolean “字符串未被识别为有效的布尔值。”

发布于 2024-11-04 18:45:54 字数 513 浏览 1 评论 0原文

我在使用布尔列从数据库获取记录时遇到问题。我无法更改数据库结构。
数据库类型是Character(1) (PostgreSQL),其中“t”表示 true,“f”表示 false。我用过 PostgreSQLDialect。

我试图将其放入休眠配置中,

 <property name="query.substitutions">1 't',0 'f'</property>

我试图用方言覆盖

 public override string ToBooleanValueString(bool value)
        {
            return value ? "t" : "f";
        }

映射是:

Map(x => x.IsTemplate).Column("template_p");

仍然不起作用, 有什么帮助吗?

I've got a problem when getting a record from the database with a boolean column. I can't change the database structure.
The database type is Character(1) (PostgreSQL) where they used 't' for true and 'f' for false. I have used the PostgreSQLDialect.

I've tried to put this in the hibernate-configuration

 <property name="query.substitutions">1 't',0 'f'</property>

I've tried to override in the dialect

 public override string ToBooleanValueString(bool value)
        {
            return value ? "t" : "f";
        }

The mapping is:

Map(x => x.IsTemplate).Column("template_p");

Still not working,
Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇女薄笑 2024-11-11 18:45:55

您可能必须在此处创建自己的用户类型。以下是创建您自己的示例:

http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

然后你的映射将变成这样:

Map(x => x.IsTemplate).Column("template_p").CustomType<MyCustomType>();

编辑:

您还可以通过对查询替换执行某些操作来使用标准 YesNo 类型。我还没有对此进行测试,但可能是这样的:

<property name="query.substitutions">yes 't', no 'f'</property>

您的映射看起来与我上面所述的几乎相同,只是您将使用 YesNo 类型。

You may have to create your own user type here. Here is an example of creating your own:

http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

Your mapping would then become something like this:

Map(x => x.IsTemplate).Column("template_p").CustomType<MyCustomType>();

Edit:

You might also be able to use the standard YesNo type by doing something with your query-substitiutions. I haven't tested this but maybe something like this:

<property name="query.substitutions">yes 't', no 'f'</property>

Your mapping would look pretty much the same as I stated above except you would use the YesNo type.

木緿 2024-11-11 18:45:55

事实上,Cole W,我制作了这个自定义类型,它的效果就像一个魅力
(源链接:https://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

public class TFType : IUserType
{
    public bool IsMutable
    {
        get { return false; }
    }

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

    public SqlType[] SqlTypes
    {
        get { return new[]{NHibernateUtil.String.SqlType}; }
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if(obj == null ) return null;

        var truefalse = (string) obj;

        if( truefalse != "t" && truefalse != "f" )
            throw new Exception(string.Format("Expected data to be 't' or 'f' but was '{0}'.", truefalse));

        return truefalse == "t";
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        if(value == null)
        {
            ((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
        }
        else
        {
            var yes = (bool) value;
            ((IDataParameter)cmd.Parameters[index]).Value = yes ? "t" : "f";
        }
    }

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

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

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

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

    public new bool Equals(object x, object y)
    {
        if( ReferenceEquals(x,y) ) return true;

        if( x == null || y == null ) return false;

        return x.Equals(y);
    }

    public int GetHashCode(object x)
    {
        return x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode();
    }
}

Indeed Cole W, i made this customtype and it works like a charm
(source link: https://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/)

public class TFType : IUserType
{
    public bool IsMutable
    {
        get { return false; }
    }

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

    public SqlType[] SqlTypes
    {
        get { return new[]{NHibernateUtil.String.SqlType}; }
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if(obj == null ) return null;

        var truefalse = (string) obj;

        if( truefalse != "t" && truefalse != "f" )
            throw new Exception(string.Format("Expected data to be 't' or 'f' but was '{0}'.", truefalse));

        return truefalse == "t";
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        if(value == null)
        {
            ((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
        }
        else
        {
            var yes = (bool) value;
            ((IDataParameter)cmd.Parameters[index]).Value = yes ? "t" : "f";
        }
    }

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

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

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

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

    public new bool Equals(object x, object y)
    {
        if( ReferenceEquals(x,y) ) return true;

        if( x == null || y == null ) return false;

        return x.Equals(y);
    }

    public int GetHashCode(object x)
    {
        return x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode();
    }
}
弥枳 2024-11-11 18:45:55

没有尝试过,但也许这可以工作:

Map(x => x.IsTemplate).Formula("case when template_p = 't' then 1 else 0 end")

Haven't tried, but maybe this could work:

Map(x => x.IsTemplate).Formula("case when template_p = 't' then 1 else 0 end")
人生戏 2024-11-11 18:45:55

我们使用带有布尔值的 postgres 8.3-8.4,没有任何问题。然而,当使用基于 ANTLR3 的 HQL 解析器时,它开始对布尔替换变得挑剔,因此我们使用以下覆盖创建了自己的方言:

public override string ToBooleanValueString( bool value )
{
    return value ? "true" : "false";
}

但是,我们使用 NHibernate.Dialect.PostgreSQL82Dialect,而不是通用的。你运行的 postgres 版本是什么?

编辑:哦,抱歉,我没有注意到您有一个字符(1)列。也许这会有所帮助(带引号)?

return value ? "'t'" : "'f'";

We are using postgres 8.3-8.4 with boolean values without any problems. However, when using ANTLR3 based HQL parser it started to get picky about boolean substitution, so we made our own dialect with the following override:

public override string ToBooleanValueString( bool value )
{
    return value ? "true" : "false";
}

However, we used NHibernate.Dialect.PostgreSQL82Dialect, not the generic one. What postgres version are you running?

EDIT: Oh, sorry, I didn't follow that you hade a character(1) column. maybe this would help instead (with the quotes)?

return value ? "'t'" : "'f'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文