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
最满意答案
以下是
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], ...
-
C UINT16如何搞定?(C UINT16 How to get it right?)[2023-01-22]
您将传递给整数的AddToLog和snprintf指针。 所以你看到的是整数的地址,而不是整数本身。 您需要取消引用指针 - 例如,在第一种方法中,在调用AddToLog在AddToLog前放置一个星号(*)。 正如@rileyberton建议的那样,很可能unsigned int在你的系统上是4个字节,这是C99类型uint32_t 。 对于16位整数,请使用uint16_t 。 这些在stdint.h中定义。 这些传统上称为“短整数”或“半整数”,并且需要printf或类似函数中的%hu限定符,而不仅仅 ... -
uint16_t vs UInt16(uint16_t vs UInt16)[2023-01-17]
在标准类型成为标准的一部分之前,存在“用户定义的”类型。 我在任何地方都使用标准类型,除非直接使用指定“用户定义”类型的接口。 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")); ...
-
在C ++中使用uint16?(fread-ing uint16 in C++?)[2023-07-24]
你可能想要: 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, - ...
-
将QString转换为uint16 *(Convert QString to uint16*)[2022-02-20]
尝试使用utf16 ()方法 - 比如WebShot_Open (WebShotHandle, url.utf16 ()) Try to use utf16 () method - like WebShot_Open (WebShotHandle, url.utf16 ())