2011-05-26 97 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);"); 

    ... 
} 

更好的辦法,仍然歡迎