nhibernate 将多行映射到一个对象

发布于 2024-11-09 23:22:31 字数 2233 浏览 3 评论 0原文

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

编辑:由于历史原因重新编写描述,

文本不会作为文本存储在列中,而是保存在表中。我有几个具有以下结构的表格:

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

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

到目前为止我的解决方案:

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

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”,

感谢您的时间

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2024-11-16 23:22:31

我发现了一种相当丑陋的方法,可能不太便携

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

    ...
}

仍然欢迎更好的方法

i found a rather ugly way which is probably not very portable

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

    ...
}

Better ways are still welcome

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文