[英]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: 映射取决于地址和用户之间的关系是什么:
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.
映射还取决于您要如何创建用户和地址类。
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.