如何在nhibernate中映射接口?
我正在使用两个类 NiceCustomer
& RoughCustomer
实现接口 ICustomer
。
ICustomer
有四个属性。它们是:
Property Id() As Integer
Property Name() As String
Property IsNiceCustomer() As Boolean
ReadOnly Property AddressFullText()作为 String
我不知道如何将接口 ICustomer
映射到数据库。
我在内部异常中收到这样的错误。
关联引用未映射的类:ICustomer
我正在使用 Fluent 和 NHibernate。
I'm using two class NiceCustomer
& RoughCustomer
which implment the interface ICustomer
.
The ICustomer
has four properties. They are:
Property Id() As Integer
Property Name() As String
Property IsNiceCustomer() As Boolean
ReadOnly Property AddressFullText() As String
I don't know how to map the interface ICustomer
, to the database.
I get an error like this in the inner exception.
An association refers to an unmapped class: ICustomer
I'm using Fluent and NHibernate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过在配置阶段插入 EmptyInterceptor,您可以直接映射到 NHibernate 中的接口。该拦截器的工作是为您在映射文件中定义的接口提供实现。
此后,所有关系和映射都可以定义为接口。
如果您想实现 ORM 的真正解耦,将所有配置/映射放置在单独的项目中并且仅引用接口,那么这种类型的技术非常有用。这样,您的域层就不会受到 ORM 的污染,如果需要,您可以在稍后阶段替换它。
You can map directly to interfaces in NHibernate, by plugging in an EmptyInterceptor during the configuration stage. The job of this interceptor would be to provide implementations to the interfaces you are defining in your mapping files.
After this, all relationships and mappings can be defined as interfaces.
This type of technique is great if you want to achieve true decoupling of your ORM, placing all configuration/mappings in a seperate project and only referencing interfaces. Your domain layer is then not being polluted with ORM, and you can then replace it at a later stage if you need to.
你如何查询?如果您使用 HQL,则需要使用 HBM 文件导入接口的命名空间,其中包含以下行:
如果您使用 Criteria,您应该只能查询 ICustomer,它将返回两种客户类型。
如果您通过 HasMany、HasManyToMany 或 References 映射一个具有客户的类,那么您需要使用通用形式:
如果您希望它能够处理其中任何一个,您需要将它们设为子类,
在这种情况下我认为您需要基类 Customer 并将其用作外部类中的通用类型参数:
无论如何,您不应该更改域模型来应对这种情况,它仍然应该在外部类上有一个 ICustomer 。
我不确定 1.0RTM 是否具有适用于参考文献的通用形式,但快速扫描更改应该会显示更改,我认为这是两行添加。
how are you querying? If you're using HQL you need to import the interface's namespace with an HBM file with this line:
If you're using Criteria you should just be able to query for ICustomer and it'll return both customer types.
If you're mapping a class that has a customer on it either through a HasMany, HasManyToMany or References then you need to use the generic form:
If you want it to cope with either, you'll need to make them subclasses
In which case I think you'll need the base class Customer and use that for the generic type parameter in the outer class:
Regardless, you shouldn't change your domain model to cope with this, it should still have an ICustomer on the outer class.
I'm not sure if the 1.0RTM has the Generic form working for References but a quick scan of the changes should show the change, which I think is a two line addition.
在 nhibernate 中映射接口是不可能的。如果您的目标是能够使用通用类型进行查询以检索两种类型的客户,则可以使用多态查询。只需让两个类都实现接口并正常映射类即可。请参阅此参考:
https://www.hibernate.org/hib_docs/nhibernate/ html/queryhql.html(第 11.6 节)
It is not possible to map an interface in nhibernate. If your goal is to be able to query using a common type to retrieve both types of customers you can use a polymorphic query. Simply have both your classes implement the interface and map the classes normally. See this reference:
https://www.hibernate.org/hib_docs/nhibernate/html/queryhql.html (section 11.6)