2011-05-26 93 views
1

我有一个相当困难的映射问题。nhibernate将许多行映射到一个对象

编辑:重新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) 
) 

文本被保存为与任意长度,不同献给每个实体即使在一个表中不同texttypes线和有时。 我想将它们映射到内部和映射中处理这些怪癖的类。

我的解决方案迄今:

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

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");,但我得到异常“意外的行数-1,预计1”

感谢您的时间

+0

我很抱歉,我看了你的问题两次,但没看懂它。你能把重点放在问题是什么吗? – 2011-05-26 13:02:19

+0

@Ilya Kogan希望现在更清楚 – Firo 2011-05-26 14:01:34

回答

0

我发现了一个相当丑陋的方式,可能不是很便携

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);"); 

    ... 
} 

更好的办法,仍然欢迎