简体   繁体   English

如何使用Mapping.ByCode从Oracle序列中获取NHibernate复合键的值?

[英]How can I source an NHibernate composite key's values from an Oracle sequence using Mapping.ByCode?

Using NHibernate's Mapping.ByCode , how can I source the initial values of a composite key from Oracle sequences? 使用NHibernate的Mapping.ByCode ,如何从Oracle序列中获取复合键的初始值?

ComposedId(map =>
{
    map.Property(x => x.Column1);
    map.Property(x => x.Column2);
});

The below works for a single key, but ComposedId uses IComposedIdMapper , which doesn't seem to have an equivalent to IIdMapper 's Generator : 下面的代码适用于单个键,但是ComposedId使用IComposedIdMapper ,它似乎没有与IIdMapperGenerator等效:

Id<decimal>(x => x.Id, o =>
{
    o.Column("ID");
    o.Generator(Generators.Native, genMapping => genMapping.Params(new { sequence = "MY_SCHEMA.MY_ID_SEQ" }));
});

If one column is generated from a sequence incremented at each insert, it ought to be unique alone. 如果从每个插入处递增的序列生成一列,则该列应该唯一。 So it could be mapped as a single column id. 因此可以将其映射为单个列ID。

Composite id does not allow for generators because they are by essence assigned, by example to some primary keys of other tables, or to some enumerated value like a "type of the entity" combined a key of wome owner, a rank, whatever. 组合ID不允许生成器,因为生成器本质上被分配了(例如)其他表的某些主键,或分配给某些枚举值(例如“实体的类型”),并结合了所有者所有者的键,等级等。

You may still use a sequence for assigning one part of the composite id if you really have to, but you will have to call by yourself the sequence for getting its value and assigning it to your composite id. 如果确实需要,您仍然可以使用序列来分配复合ID的一部分,但是您将必须自己调用序列以获取其值并将其分配给复合ID。 Use ISession.CreateSQLQuery("adequate sql for your db") for doing this. 使用ISession.CreateSQLQuery("adequate sql for your db")执行此操作。 You may build your sql with the help of the dialect object, exposed by your session factory. 您可以在会话工厂公开的方言对象的帮助下构建sql。 (Call ISession.GetSessionImplementation().Factory.Dialect.GetSequenceNextValString("sequenceName") .) (调用ISession.GetSessionImplementation().Factory.Dialect.GetSequenceNextValString("sequenceName") 。)

But normally, if you want to use a sequence for an id, use a single column id, not a composite one. 但是通常,如果要对ID使用序列,请使用单列ID,而不要使用复合ID。

It looks to me quite wrong to use a composite-id with one column generated with a unique value: better avoid composite id whenever possible. 在我看来,使用带有唯一值生成的一列的Composite-id是完全错误的:最好尽可能避免使用Composite ID。 If the value is unique, it does not need any additional column for being a primary key. 如果该值是唯一的,则不需要任何其他列作为主键。

By the way, in most cases, NHibernate will not care whether the mapped id is actually declared as a primary in the database or not, whether an association key is declared as a foreign key or not, ... It matters only if you use NHibernate for schema generation. 顺便说一句,在大多数情况下,NHibernate不会在乎映射的ID是否实际上在数据库中声明为主键,关联键是否声明为外键,...仅在您使用时才重要NHibernate用于模式生成。 So better map them in the way that makes more sense, unless you have some weird things preventing you to simplify them in the NHibernate mapping. 因此,最好以更有意义的方式映射它们,除非您有一些奇怪的事情阻止您在NHibernate映射中简化它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何使用 NHibernate Mapping.ByCode 在非主键字段上加入表? - How would I join a table on a non primary key field using NHibernate Mapping.ByCode? 如何在NHibernate ByCode中分别映射相同类型的两个组件属性? - How can I map two component properties of the same type separately in NHibernate ByCode? NHibernate映射带有复合键的子级 - NHibernate mapping child with composite key 流利的NHibernate映射/使用组合键删除父/子项失败 - Fluent NHibernate Mapping / Parent/Child delete fail using composite key 使用NHibernate,如何给定父母的ID来检索复合元素对象的列表? - Using NHibernate, how can I retrieve a list of composite-element objects given a parent's ID? 使用Nhibernate.Mapping.Attributes指定复合键 - Specify composite key using Nhibernate.Mapping.Attributes 不能使用 xml 映射的连接子类来扩展使用 NHibernate.Mapping.ByCode 构建的映射 - Can not use joined-subclass of xml mapping to extend the mappings build with NHibernate.Mapping.ByCode nhibernate中使用复合键进行多对一映射 - many-to-one mapping with composite key in nhibernate 使用NHibernate的ISession.Get &lt;&gt;()w /复合键 - Using NHibernate's ISession.Get<>() w/ a composite key 使用 Fluent NHibernate 映射引用到复合键的问题 - Problem mapping reference to composite key with Fluent NHibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM