Nhibernate - 如何使用 CompositeId 设计 DomainObjects 和映射
起初我对 ORM、nhibernate 和 FHN 还很陌生。我已经研究这个东西几天了。 我有一个包含 4 个表的现有数据库:
所以我首先尝试让 Auswahl 和 RefAuswahlFilter 工作。
这是我的 DomainObjects:请告诉我何时可以做得更好
[Serializable]
public class Auswahl
{
public Auswahl()
{
this.RefFilters = new List<RefAuswahlFilter>();
}
public virtual IList<RefAuswahlFilter> RefFilters { get; set; }
//...rest of the Properties
[Serializable]
public class RefAuswahlFilter
{
public virtual Auswahl Auswahl { get; set; }
public virtual long Filterrank { get; set; }
public virtual string Filter { get; set; }
这是我的映射:
public class AuswahlMap : ClassMap<Auswahl>
{
public AuswahlMap()
{
Table("AUSWAHL");
Id(x => x.Id)
.GeneratedBy.Sequence("SEQ_AUSWAHL");
Map(x => x.Programm);
Map(x => x.Variante);
Map(x => x.Returnkey);
Map(x => x.Beschreibung);
HasMany<RefAuswahlFilter>(x => x.RefFilters);
}
}
public class RefAuswahlFilterMap : ClassMap<RefAuswahlFilter>
{
public RefAuswahlFilterMap()
{
Table("REFAUSWAHLFILTER");
CompositeId()
.KeyReference(x => x.Auswahl,"IDAUSWAHL")
.KeyProperty(x => x.Filterrank,"FILTERANK");
Map(x => x.Filter);
}
}
如果我运行我的应用程序,我会得到一个包含所有 Auswahl 行的列表。
var l = session.CreateCriteria(typeof(Auswahl)).List<Auswahl>();
但是当我查看一个项目以获取 RefFilters 时,调试器显示以下错误:
innerException {"ORA-00904: \"REFFILTERS0\".\"AUSWAHL_ID\": ungültiger Bezeichner\n"}
无法初始化集合:[ORMTest.DomainModel.Auswahl.RefFilters#1097][SQL:选择 reffilters0_.Auswahl_id 作为 Auswahl4_1_、reffilters0_.IDAUSWAHL 作为 IDAUSWAHL1_、reffilters0_.FILTERANK 作为 FILTERANK1_、reffilters0_.IDAUSWAHL 作为 IDAUSWAHL3_0_、ref过滤器0_.FILTERANK 为 FILTERANK3_0_ , reffilters0_.Filter as Filter3_0_ FROM REFAUSWAHLFILTER reffilters0_ WHERE reffilters0_.Auswahl_id=?]
所以我认为我的映射上的某些内容是错误的,但我真的不明白。
谢谢。
at first i'm really new to ORM, nhibernate and FHN. i look into this stuff for a few days now.
i have an existing database with 4 tables:
so i tried first to get the Auswahl and RefAuswahlFilter to work.
here are my DomainObjects: pls let me know when i can do this better
[Serializable]
public class Auswahl
{
public Auswahl()
{
this.RefFilters = new List<RefAuswahlFilter>();
}
public virtual IList<RefAuswahlFilter> RefFilters { get; set; }
//...rest of the Properties
[Serializable]
public class RefAuswahlFilter
{
public virtual Auswahl Auswahl { get; set; }
public virtual long Filterrank { get; set; }
public virtual string Filter { get; set; }
here are my mappings:
public class AuswahlMap : ClassMap<Auswahl>
{
public AuswahlMap()
{
Table("AUSWAHL");
Id(x => x.Id)
.GeneratedBy.Sequence("SEQ_AUSWAHL");
Map(x => x.Programm);
Map(x => x.Variante);
Map(x => x.Returnkey);
Map(x => x.Beschreibung);
HasMany<RefAuswahlFilter>(x => x.RefFilters);
}
}
public class RefAuswahlFilterMap : ClassMap<RefAuswahlFilter>
{
public RefAuswahlFilterMap()
{
Table("REFAUSWAHLFILTER");
CompositeId()
.KeyReference(x => x.Auswahl,"IDAUSWAHL")
.KeyProperty(x => x.Filterrank,"FILTERANK");
Map(x => x.Filter);
}
}
if i run my app i got a list with all my Auswahl rows.
var l = session.CreateCriteria(typeof(Auswahl)).List<Auswahl>();
but when i look into an item to get the RefFilters the debugger show the following error:
innerException {"ORA-00904: \"REFFILTERS0\".\"AUSWAHL_ID\": ungültiger Bezeichner\n"}
could not initialize a collection: [ORMTest.DomainModel.Auswahl.RefFilters#1097][SQL: SELECT reffilters0_.Auswahl_id as Auswahl4_1_, reffilters0_.IDAUSWAHL as IDAUSWAHL1_, reffilters0_.FILTERANK as FILTERANK1_, reffilters0_.IDAUSWAHL as IDAUSWAHL3_0_, reffilters0_.FILTERANK as FILTERANK3_0_, reffilters0_.Filter as Filter3_0_ FROM REFAUSWAHLFILTER reffilters0_ WHERE reffilters0_.Auswahl_id=?]
so i think something on my mapping is wrong, but i really dont get it.
thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我部分成功了。现在可以从数据库读取。
到目前为止,保存到数据库只能使用空列表(RefFilters),所以我现在必须研究这个。这是来自我的日志。我真的不知道“NHibernate.Type.Int64Type - 将'1'绑定到参数:3 14:31:18.585 [9] DEBUG”来自哪里,但这是迄今为止我得到的例外。
14:31:18.397 [9] 调试 NHibernate.Persister.Entity.AbstractEntityPersister - 插入实体:[ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter]
14:31:18.413 [9] 调试 NHibernate.AdoNet.AbstractBatcher - 打开新的 IDbCommand,打开 IDbCommands:1
14:31:18.413 [9] 调试 NHibernate.AdoNet.AbstractBatcher - 为 SqlString 构建 IDbCommand 对象:INSERT INTO REFAUSWAHLFILTER (Filter, IDAUSWAHL, Filterrank) VALUES (?, ?, ?)
14:31:18.413 [9] 调试 NHibernate.Persister.Entity.AbstractEntityPersister - 脱水实体:[ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter]
14:31:18.413 [9] 调试 NHibernate.Type.StringType - 将“fh”绑定到参数:0
14:31:18.413 [9] 调试 NHibernate.Type.Int64Type - 将“1446”绑定到参数:1
14:31:18.413 [9] 调试 NHibernate.Type.Int64Type - 将“1446”绑定到参数:2
14:31:18.413 [9] 调试 NHibernate.Type.Int64Type - 将“1”绑定到参数:3
14:31:18.585 [9] 调试 NHibernate.AdoNet.AbstractBatcher - 关闭 IDbCommand,打开 IDbCommands:0
14:31:18.647 [9] 调试 NHibernate.AdoNet.ConnectionManager - 注册刷新结束
编辑:
如果我将映射更改为
按预期保存作品。尽管如此,NHibernate Profiler 显示了一些警告,并且我对 RefAuswahlFilter 的选择看起来不太好:)
well i was partially successful. reading from the db works now.
Saving to DB just works with an empty List (RefFilters) till now, so i have to look into this now. this is from my log. i really dont know where "NHibernate.Type.Int64Type - binding '1' to parameter: 3 14:31:18.585 [9] DEBUG" come from but this is the exception i get so far.
14:31:18.397 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: [ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter]
14:31:18.413 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Opened new IDbCommand, open IDbCommands: 1
14:31:18.413 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Building an IDbCommand object for the SqlString: INSERT INTO REFAUSWAHLFILTER (Filter, IDAUSWAHL, Filterrank) VALUES (?, ?, ?)
14:31:18.413 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter]
14:31:18.413 [9] DEBUG NHibernate.Type.StringType - binding 'fh' to parameter: 0
14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1446' to parameter: 1
14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1446' to parameter: 2
14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1' to parameter: 3
14:31:18.585 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Closed IDbCommand, open IDbCommands: 0
14:31:18.647 [9] DEBUG NHibernate.AdoNet.ConnectionManager - registering flush end
EDIT:
if i change my mapping to
saving works like expected. nevertheless the NHibernate Profiler shows some warnings and my select for RefAuswahlFilter looks not that good :)