问题描述
我有一个相当困难的映射问题。
编辑:重新制定
由于历史原因,文本不会作为文本存储在列中,而是存储在表中。我有几个具有以下结构的表:
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();
;
}
我的问题是,只读不会阻止nhibernate在保存时插入它。我可以做些什么来使其正常工作,还是有人对更合理的域对象有更好的主意?
Edit2:我摆弄SQLInsert(\"SELECT 1\");
,但出现异常“意外行数-1,期望1”
谢谢你的时间
解决方法
我发现了一种很丑陋的方式,可能不太便携
public TextmoduleMap()
{
...
ReadOnly();
SqlInsert(\"DROP TABLE IF EXISTS temp; CREATE TEMP TABLE temp(id int); INSERT INTO temp (id) VALUES (1);\");
SqlDelete(\"DROP TABLE IF EXISTS temp; CREATE TEMP TABLE temp(id int); INSERT INTO temp (id) VALUES (1);\");
...
}
仍然欢迎更好的方法