首页 \ 问答 \ nhibernate将许多行映射到一个对象(nhibernate mapping many rows to one object)

nhibernate将许多行映射到一个对象(nhibernate mapping many rows to one object)

我有一个相当困难的映射问题。

编辑:重新制定的descritpion

由于历史原因,文本不作为文本存储在列中,而是保存在表中。 我有几个表具有以下结构:

TABLE SomeEntityTexts
(
  id serial NOT NULL,
  key character varying(10),      // \
  linenumber integer,             // / unique constraint
  type smallint,
  length smallint,                // actual length of content string
  content character varying(80),
  PRIMARY KEY (id)
)

文本保存为具有任意长度的行,每个实体不同,有时甚至是一个表中的不同文本类型。 我想将它们映射到在内部和映射中处理这些怪癖的类。

到目前为止我的解

隐藏的集合和虚拟对象应该是只读的。 对于加载,总是有效的Text对象,因为持久化内部集合会创建它们。

internal class Textline
{
    public virtual int Id { get; set; }

    public virtual TextType Type { get; set; }   // Enum

    public virtual int Linenumber { get; set; }
    public virtual string Text { get; set; }
}

public class Textmodule
{
    public virtual int Id { get; set; }

    public virtual string Key { get; set; }      // Unique
    public virtual TextType Type { get; set; }   // Enum

    protected internal virtual IList<Textline> Textlines { get; set; }
    public virtual string Text
    {
        get { Textlines.select(t => t.Text).Aggregate(/* ...*/); }
        set { /* split text to lines with max 80 chars and feed to Textlines*/}
    }
}

public TextmoduleMap()
{
    Table("textmodules");
    ReadOnly();    // problem: shouldnt insert and update at all, but does insert
    Where("linenumber = 1");  // starts with 1

    // doesnt matter because it shouldnt be saved
    Id(text => text.Id, "id").GeneratedBy.Custom<SimpleGenerator>();

    Map(text => text.Key);
    HasMany(text => text.Textzeilen)
        .Table("textmodules")
        .PropertyRef("Key")
        .KeyColumn("key")
        .Component(c =>
        {
            c.Map(line => line.Text)
                .Columns.Add("content", "length")
                .CustomType<StringWithLengthUserType>();
            c.Map(line => line.Linenumber, "linenumber");
        })
        .Cascade.AllDeleteOrphan()
        .Not.LazyLoad();
        ;
}

我的问题是,Readonly不会阻止nhibernate在保存时插入它。 有什么我可以做的让它工作或有人有更好的想法更健全的域对象?

Edit2:我摆弄了SQLInsert("SELECT 1"); 但我得到异常“意外的rowcount -1,期望1”

谢谢你的时间


i have a rather difficult mapping problem.

EDIT: reformulated descritpion

for historical reasons texts are not stored in a column as text, instead they are saved in tables. i have several tables with following structure:

TABLE SomeEntityTexts
(
  id serial NOT NULL,
  key character varying(10),      // \
  linenumber integer,             // / unique constraint
  type smallint,
  length smallint,                // actual length of content string
  content character varying(80),
  PRIMARY KEY (id)
)

text is saved as lines with arbitrary length, different für each entity and sometimes even for different texttypes in one table. i would like to map them to classes which handle these quirks inside and in mappings.

my solution so far:

a hidden collection and a dummy object which should be readonly. For loading there are always valid Text-objects because persisting the inner collection creates them.

internal class Textline
{
    public virtual int Id { get; set; }

    public virtual TextType Type { get; set; }   // Enum

    public virtual int Linenumber { get; set; }
    public virtual string Text { get; set; }
}

public class Textmodule
{
    public virtual int Id { get; set; }

    public virtual string Key { get; set; }      // Unique
    public virtual TextType Type { get; set; }   // Enum

    protected internal virtual IList<Textline> Textlines { get; set; }
    public virtual string Text
    {
        get { Textlines.select(t => t.Text).Aggregate(/* ...*/); }
        set { /* split text to lines with max 80 chars and feed to Textlines*/}
    }
}

public TextmoduleMap()
{
    Table("textmodules");
    ReadOnly();    // problem: shouldnt insert and update at all, but does insert
    Where("linenumber = 1");  // starts with 1

    // doesnt matter because it shouldnt be saved
    Id(text => text.Id, "id").GeneratedBy.Custom<SimpleGenerator>();

    Map(text => text.Key);
    HasMany(text => text.Textzeilen)
        .Table("textmodules")
        .PropertyRef("Key")
        .KeyColumn("key")
        .Component(c =>
        {
            c.Map(line => line.Text)
                .Columns.Add("content", "length")
                .CustomType<StringWithLengthUserType>();
            c.Map(line => line.Linenumber, "linenumber");
        })
        .Cascade.AllDeleteOrphan()
        .Not.LazyLoad();
        ;
}

My problem is, that Readonly doesnt prevent nhibernate from inserting it on save. Is there anything i can do to get it work or does someone has a better idea for a more sane domain object?

Edit2: I fiddled with SQLInsert("SELECT 1"); but i get exception "unexpected rowcount -1, expect 1"

thanks for your time


原文:https://stackoverflow.com/questions/6135909
更新时间:2023-08-24 16:08

最满意答案

以下是Convert.ToUInt16(int value)完整源代码

    [CLSCompliant(false)]   
    public static ushort ToUInt16(int value) {
        if (value < 0 || value > UInt16.MaxValue) 
            throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
        Contract.EndContractBlock();
        return (ushort)value;
    }

无论如何它看起来像是演员。


Here is the full source for Convert.ToUInt16(int value) :

    [CLSCompliant(false)]   
    public static ushort ToUInt16(int value) {
        if (value < 0 || value > UInt16.MaxValue) 
            throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
        Contract.EndContractBlock();
        return (ushort)value;
    }

Looks like it resolves to a cast anyway.

相关问答

更多
  • 不幸的是,除了CharStream以外,不可能使用FParsec。 我使用这篇文章自己编写了一个简单的解析器组合器来解决这个问题。 令人惊讶的是,这只是一天的工作。 在这个过程中,我学到了很多关于解析器组合器的知识。 Unfortunately it is not possible to use FParsec on anything else than a CharStream. I solved the problem by writing a simple parser combinator myse ...
  • 我认为你是在BitConverter类之后。 http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx float value = 1.1f; byte[] bytes = BitConverter.GetBytes(value); short upper = BitConverter.ToInt16(bytes, 0); short lower = BitConverte ...
  • 根据机器的字节顺序,它可以是简单的视图转换或字节交换,然后是视图转换: >>> a = np.random.randint(0, 2, (4, 16)) >>> a array([[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1], [0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1], ...
  • 您将传递给整数的AddToLog和snprintf指针。 所以你看到的是整数的地址,而不是整数本身。 您需要取消引用指针 - 例如,在第一种方法中,在调用AddToLog在AddToLog前放置一个星号(*)。 正如@rileyberton建议的那样,很可能unsigned int在你的系统上是4个字节,这是C99类型uint32_t 。 对于16位整数,请使用uint16_t 。 这些在stdint.h中定义。 这些传统上称为“短整数”或“半整数”,并且需要printf或类似函数中的%hu限定符,而不仅仅 ...
  • 在标准类型成为标准的一部分之前,存在“用户定义的”类型。 我在任何地方都使用标准类型,除非直接使用指定“用户定义”类型的接口。 The "user-defined" types existed before the standard types became part of the standard. I use the standard types everywhere, except when directly using an interface that specifies the "user-de ...
  • 以下是Convert.ToUInt16(int value)的完整源代码 : [CLSCompliant(false)] public static ushort ToUInt16(int value) { if (value < 0 || value > UInt16.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_UInt16")); ...
  • 你可能想要: fread(ptr, sizeof(uint8), 2, file) 好像对我来说, fread(ptr, 1, 2, file)甚至更好。 或者fread(ptr, sizeof(uint16_t), 1, file) 。 我假设ptr是一个指向你想要存储数据的位置的指针,而不是变量本身(如果是这样 - 使用&ptr )。 虽然一次读取两个字节可能会成为性能问题,但请考虑读入内存缓冲区,然后解析它。 You probably want: fread(ptr, sizeof(uint8), ...
  • 这很大程度上取决于输入图像的确切格式。 只是将“转换为单词”并不是真正表达你想要做的事情,这可能更像是“将位图图像转换为行主格式的RGB565像素数组”。 您应该查看允许您加载位图图像的图像处理库,并读出每个像素的值。 您可以直接转换为RGB565,不应该与任何其他位图格式相比太难。 请注意,有索引和“真彩色”位图格式,你听起来像你需要处理两者。 如果幸运的话,每种格式的库都会将其抽象出来并具有例如uint32 read_pixel_as_rgb888()函数。 另请注意,许多位图图像格式都非常关注压缩,这 ...
  • Javascript中的所有数字变量都被视为64位浮点数(双精度数)。 一旦从DataView将其读入变量,它就会以该形式出现。 DataView对象上的set*方法允许您使用各种打包形式在ArrayBuffer中设置字节: dv.setUint16(0, 12); dv.getUint16(0); // >>> 12 但你必须小心自己夹紧它: dv.setUint16(0, -12); dv.getUint16(0); // >>> 65524 对于签名的int16: dv.setInt16(0, - ...
  • 尝试使用utf16 ()方法 - 比如WebShot_Open (WebShotHandle, url.utf16 ()) Try to use utf16 () method - like WebShot_Open (WebShotHandle, url.utf16 ())

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)