有没有办法定义 n-hibernate 映射的可重用属性?

发布于 2024-08-10 03:39:48 字数 707 浏览 3 评论 0 原文

我有一个场景,我想向我的实体添加一些标准属性。这意味着我将有 1 个 int 和 2 个字符串属性应用于所有相关实体。我有 100 多个映射文件,其中大多数(但不是全部)将成为这些新属性的主机。在类中很容易定义这一点;然而,在映射中,除了创建实用程序或 xslt 并应用它之外,我没有找到任何参考(如何在 hibernate 映射中定义可重用的 元素)。

但是我希望能够从这个“标准”映射中添加/修改/删除属性。

感谢您的任何回复

Edit1:我要添加的映射示例

<property name="TimeOfEdit" column="TimeOfEdit" type="DateTime" not-null="true"/>
<many-to-one name="EditedBy" column="FK_EditedBy" cascade="save-update" not-null="true" />

Edit2: 我删除了已接受的解决方案,因为 NH 2.1.1 XML 实体不起作用 (NH-1236) 并且 NH 将抛出“此 XML 文档中禁止 DTD”

I have a scenario that i want to add some standard properties to my entities. Meaning that i will have e.g. 1 int and 2 string properties applied to all relevant entities. I have over 100 mapping files and most but not all will be hosts to these new properties. In the classes its easy to define this; in the mappings however i've found no reference other than creating a utility or xslt and applying that (How to define reusable <generator> elements in hibernate mapping).

However i want to be able to add/modify/remove properties from this "standard" mapping.

thx for any replies

Edit1: an example of the mapping i want to add

<property name="TimeOfEdit" column="TimeOfEdit" type="DateTime" not-null="true"/>
<many-to-one name="EditedBy" column="FK_EditedBy" cascade="save-update" not-null="true" />

Edit2:
I removed the accepted solution because with NH 2.1.1 XML Entities are not working (NH-1236) and NH will throw a "DTD is prohibited in this XML document"

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

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

发布评论

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

评论(5

依 靠 2024-08-17 03:39:48

这取决于这些属性在您的类中的实现方式。

如果它们都在基类或接口中定义,您可以在基类或接口中将它们映射一次,然后使用union-subclass进行派生。有一些限制。阅读NHibernate 文档中的这一章有关于它的内容。

如果您决定将它们放在一个类中,则可以将它们映射为用户类型。这与组件类似,但您可以在用户类型中指定一些内容,例如类型名称、长度等。您仍然需要指定每个列名称。

还有另一种选择:您可以使用XML 实体。这是 NHibernate 支持的 XML 的一个相当原始的功能。阅读NH 参考文档中的这一章 中提到了这一点。

It depends on how these properties are implemented in your classes.

If they are all defined in a base class or interface, you could map them once in the base class or interface, and derive using union-subclass. There are some limitations. Read this chapter in the NHibernate documentation about it.

If you decide to put them together into a class, you could map them as a user type. This will be similar to a component, but you could specify some things like type names, lengths and others in the user type. You still need to specify each column name.

There is another option: you could use XML entities. This is a rather primitive feature from XML which is supported by NHibernate. Read this chapter in the NH reference documentation where it is mentioned.

快乐很简单 2024-08-17 03:39:48

为您的具体情况创建特殊的代码生成器是您唯一的选择。

Creating a special code generator for your specific case is your only option.

探春 2024-08-17 03:39:48

选项 1:
- 在基类中定义这 3 个属性

- 让您的实体从此基类继承 -

设置“每个类层次结构表”

选项 2:

- 将这 3 个属性定义为组件。

- 您可以将这 3 个属性的映射放在一个可重复使用的文件中。

Option 1:
-Define these 3 properties in a base class

-have your entities inherit from this base

-set up 'table per class hierarchy'

Option 2:

-Define these 3 properties as a component.

-You can have the mapping for these 3 properties in one file that is reused.

随梦而飞# 2024-08-17 03:39:48

你可以看看 fluenceNHibernate,它会简化你的映射工作。使用自动映射,您可能只需要一个抽象基类来定义这些属性。

You might take a look at fluentNHibernate, It will simplify the mapping work for you. With With auto mapping you may only need an abstract base class to define these properties.

请恋爱 2024-08-17 03:39:48

似乎唯一要做的就是使用动态映射(http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx),

因为我已经定义了我的实体的接口将用于新属性(假设是 IAuditable),只需在 NH 会话初始化时运行适当的代码

Configuration cfg = new Configuration() Mappings mappings = cfg.CreateMappings(); 
foreach (var persistentClass in mappings.Classes) 
{ 
   if (persistentClass.MappedClass is IAuditable)
   {
     ...
   }
}

,然后

cfg.BuildSessionFactory();

将其连接起来并准备使用即可
对于大约 85 个类,性能影响可以忽略不计

It seems that the only to do this, is to use Dynamic Mapping (http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx)

as such since i've already defined an interface that my entities will use for the new properties (lets say IAuditable) its just a matter of running the appropriate code at the NH-session initialization

Configuration cfg = new Configuration() Mappings mappings = cfg.CreateMappings(); 
foreach (var persistentClass in mappings.Classes) 
{ 
   if (persistentClass.MappedClass is IAuditable)
   {
     ...
   }
}

and then

cfg.BuildSessionFactory();

to have it wired up and ready to be used
for about 85 classes the performance impact is negligible

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