简体   繁体   English

nhibernate如何处理本质上不是实体的表?

[英]How would nhibernate handle tables that aren't really entities per se?

I understand how nhibernate would map a table like Users, and Addresses. 我知道nhibernate将如何映射一个表,如用户和地址。

But what if I have a table like: 但是如果我有一个像这样的表怎么办:

Users_Addresses with columns UserID, AndressID. Users_Addresses列为UserID,AndressID。

Would I have to create a mapping for this and make it like a normal entity? 我是否需要为此创建一个映射并使它像普通实体一样? It is really a table that I would reference in a inner join. 我实际上是在内部联接中引用的表。

With NHibernate, you design your code independent of the database layout. 使用NHibernate,您可以独立于数据库布局设计代码。 You don't have to (and don't should to) create classes that are exactly the same as your database tables and columns. 您不必(也不应该)创建与数据库表和列完全相同的类。 Example: 例:

public class User
{
    public IList<Address> Addresses { get; private set; }

    public int Id { get; set; }
}

The mapping depends on what the relationship between address and user is: 映射取决于地址和用户之间的关系是什么:

  • Can a user have multiple addresses? 一个用户可以有多个地址吗?
  • Can multiple users have the same address? 多个用户可以使用相同的地址吗?
  • What do you want to inner join on what? 您想在什么方面加入内在联系? (I would expect a many to many relationship here with an outer join) (我希望这里有一个外部联接的多对多关系)

In a scenario when a user has multiple addresses and those addresses can be used by different users, you can use many to many mapping to map the address. 在一个用户有多个地址并且那些地址可以由不同用户使用的情况下,您可以使用多对多映射来映射该地址。 The mapping also depends on how you want to create your user and address classes. 映射还取决于您要如何创建用户和地址类。

  • Is it logical to have a list of addresses in a user? 在用户中拥有地址列表是否合乎逻辑?
  • Is it logical to have a list of users in an address? 在地址中包含用户列表是否合乎逻辑?
  • Can a user have the same address twice? 用户可以两次拥有相同的地址吗?

The idea of NHibernate is: write code the way you like it, and add mapping to a database to it later. NHibernate的想法是:按照自己喜欢的方式编写代码,并在以后向其添加数据库映射。

Address is usually considered a value type in the scenario you are discussing - it has no intrinsic identity outside of the User. 在您讨论的场景中,地址通常被认为是一种值类型-地址在用户之外没有任何固有身份。

Value types in nHibernate are mapped as components. nHibernate中的值类型映射为组件。 Collections of value types are mapped as a set of composite elements. 值类型的集合映射为一组复合元素。

See https://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/components.html for details on how to do this. 有关如何执行此操作的详细信息,请参见https://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/components.html

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

相关问题 你会如何处理 nHibernate 这种情况 - How would you handle this situation with nHibernate 如何处理更新实体。 NHibernate + ASP.NET MVC - How to handle updating entities. NHibernate + ASP.NET MVC NHibernate:如何使用每个请求的会话模式处理基于实体的验证,而控制器不知道ISession - NHibernate: how to handle entity-based validation using session-per-request pattern, without controllers knowing about ISession NHibernate和上下文实体 - NHibernate and contextual entities NHibernate-基于属性/列对实体进行排序+如何管理? - NHibernate - Sorting Entities based on Property/Column + how to manage? 如何使用DbEntityEntry处理实体的状态? - How to handle the states of entities using DbEntityEntry? Fluent NHibernate 映射不适用于某些表 - Fluent NHibernate Mapping Doesn't Work For Some Tables 如何使用WebAPI / NHibernate实现每次会话会话模式 - How to implement Session Per Conversation pattern with WebAPI/ NHibernate 如何在Structuremap.MVC5中按请求管理NHibernate会话 - How to manage NHibernate Sessions per request in Structuremap.MVC5 如何在不依赖NHibernate的情况下为每个请求实现NHibernate会话? - How can I implement NHibernate session per request without a dependency on NHibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM