在流畅的 nhibernate 中映射一列两次

发布于 2024-09-19 14:16:53 字数 697 浏览 2 评论 0原文

开始了... 我有一个表(不幸的是我无法更改),其中包含以下列:

  • date
  • startTime
  • endTime

我有包含两个字段的数据类: 开始日期时间 = 日期 + 开始时间 endDateTime = date + endTime

        Map(x => x.EndDateTime)
            .Columns.Clear()
            .Columns.Add("date", "endTime")
            .CustomType<MyCustomType>();

        Map(x => x.StartDateTime)
            .Columns.Clear()
            .Columns.Add("date", "startTime")
            .CustomType<MyCustomType>();

MyCustomType 是实现 IUserType 接口的类。这似乎可行,但它仅适用于从数据库读取数据。保存或更新 NHibernate 时,将“日期”列放入两次,并且无法提交查询。

我的问题是:有什么办法可以解决这个问题吗?我希望这两个字段都是非只读的。 (将其中一个设置为只读会有所帮助,但这不是一个令我满意的解决方案)。

Here we go...
I have a table (which unfortunately I can't change) with columns like:

  • date
  • startTime
  • endTime

I have data classes with two fields:
startDateTime = date + startTime
endDateTime = date + endTime

        Map(x => x.EndDateTime)
            .Columns.Clear()
            .Columns.Add("date", "endTime")
            .CustomType<MyCustomType>();

        Map(x => x.StartDateTime)
            .Columns.Clear()
            .Columns.Add("date", "startTime")
            .CustomType<MyCustomType>();

MyCustomType is a class implementing IUserType interface. This seemed to work, but it works only with reading data from database. While saving or updating NHibernate puts column "date" twice, and query can not be commited.

My question is: is any way to go around this? I want both fields to be not-read-only. (setting one of them as read-only helps, but it's not a solution which satisfies me).

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

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

发布评论

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

评论(2

遮了一弯 2024-09-26 14:18:24

几个问题。如果两个不同的属性具有不同的值,即使它们应该转到相同的基础表字段,您会遇到什么样的错误以及您想要做什么?

Couple questions. What kind of errors do you get and what would you want to do if the two different properties have different values, even though they are supposed to go to the same underlying table field?

千仐 2024-09-26 14:18:04

好吧,我找到了一个解决方案......它不是很优雅,但是这个数据库也不是优雅:)

所以在我的 BaseController 中,它处理添加、保存、删除和更新等所有内容,我做了一些特殊的处理来保存这个特定类型:

    public virtual T Save<T>(T entity)
    {
        try
        {
            if (entity.GetType().Equals(typeof(SpecialType)))
            {
                return SaveSpecialType<T>(entity);
            }
            ITransaction transaction = session.BeginTransaction();           
            session.Save(entity);
            transaction.Commit();
            return entity;
        }
        catch (Exception ex)
        {
            if (logError.IsErrorEnabled)
                logError.Error(ex.Message + " \n " + ex.InnerException, ex);
            throw;
        }
    }

此 SaveSpecialType 方法执行本机 SQL 查询,将该混乱的类型插入数据库

Ok, i found a solution... it's not really elegant, but this database is not elegant either:)

So in my BaseController, which handles all stuff like adding, saving, deleting and updating i did some special handling of saving this particular type:

    public virtual T Save<T>(T entity)
    {
        try
        {
            if (entity.GetType().Equals(typeof(SpecialType)))
            {
                return SaveSpecialType<T>(entity);
            }
            ITransaction transaction = session.BeginTransaction();           
            session.Save(entity);
            transaction.Commit();
            return entity;
        }
        catch (Exception ex)
        {
            if (logError.IsErrorEnabled)
                logError.Error(ex.Message + " \n " + ex.InnerException, ex);
            throw;
        }
    }

This SaveSpecialType method executes native sql query inserting that messy type into database

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