对没有ID的对象使用NHibernate的最佳方法是什么? [英] What's the best way to use NHibernate for objects without ID?

查看:84
本文介绍了对没有ID的对象使用NHibernate的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些不需要ID保留的类.这些可能是用户日志或审核记录之类的东西.我可以向他们添加身份证明ID,但是我想避免这种情况,因为它们没有任何意义.

I have some classes in my app that don't require an ID to be persisted. These could be things like user logs or audit records. I can add an arbitaty id to them but I would like to avoid that as they don't mean anything.

这些对象的检索总是在另一个键(例如UserId)上,该键不是记录唯一的.

The retrieval of these objects is always on another key (like UserId) which is not unique to the record.

推荐答案

由于NHibernate处理实体,因此您必须具有标识该实体的内容.最好有一个单独的唯一id字段,因为这是NHibernate设计的工作方式,而且不打架更容易.

Because NHibernate deals with entities, you must have something that identifies the entity. It is best to have a separate unique id field as that is the way the NHibernate is designed to work and it is much easier not to fight it.

但是,如果您在设计约束上确实没有单独的id列,则可以使用在现有字段上定义的Composite-id.例如,对于审核日志记录,您可以结合使用UserId和DateTime.如果记录仅写入一次且从未被操作过(例如审核日志),则还应在类映射中添加mutable = false以强制执行该操作.另外,请注意,如果您确实使用了这种复合ID,那么将来如果您尝试将此实体使用NHibernate的一些更高级的功能,则可能会引起问题.

However, if you have design constraints where you really can't have a separate id column, you can use a composite-id defined on existing fields. For example, for an audit log record you might use a combination of a UserId and a DateTime. If the records are only written once and not ever manipulated (e.g. audit log), you should also add mutable=false to the class mapping to enforce that. Also, be aware if you do use a composite-id like this it will likely cause issues if you ever try to use some of NHibernate's more advanced features with this entity in the future.

这篇关于对没有ID的对象使用NHibernate的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆