NHibernate、数据透视表和/或 GetNextID() [英] NHibernate, Pivot-table and/or GetNextID()

查看:55
本文介绍了NHibernate、数据透视表和/或 GetNextID()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 NHibernate 中使用数据透视表吗?

Can I use pivot tables with NHibernate?

我可以写吗ID id = new Transaction().GetNextID("[TableName]", "[IDColumnName]") 类似代码用 NHibernate 增加 ID?

Can I write ID id = new Transaction().GetNextID("[TableName]", "[IDColumnName]")-like codes to increment IDs with NHibernate?

推荐答案

自动增量 ID
NHibernate 支持的所有数据库都有自动增量 id 生成器.请参阅参考文档中的generator.

要使用自增 Id 属性,请按如下方式映射它,并在创建新实体时将 Id 值保留为默认值 (0).

To use an auto-increment Id property, map it as follows and leave the Id value as the default (0) when creating new entities.

<id name="Id" >
    <generator class="native"/>
</id>

您甚至可以通过实现 NHibernate.Id.IIdentifierGenerator 来推出自己的生成器.但是,请参阅此问题以讨论为什么应该使用数据库原生自增机制.

You can even roll your own generator by implementing NHibernate.Id.IIdentifierGenerator. However, see this question for a discussion on why you should use the database native auto-increment mechanism.

数据透视表
NHibernate 没有对数据透视表的特定支持.也就是说,您可以使用结果转换器将任何 SQL 查询的结果映射到对象.目标对象不需要映射,列必须匹配属性.

Pivot Tables
NHibernate has no specific support for Pivot tables. That said, you can map the results of any SQL query to objects using a result transformer. The target object does not need to be mapped, and the columns must match the properties.

命名 SQL 查询可以在您的映射中定义文件.您还可以直接使用本机 SQL 查询.两种类型的查询都支持命名参数和位置参数.

Named SQL queries can be defined in your mapping files. You can also use native SQL queries directly. Both types of query support named and positional parameters.

var list = session.GetNamedQuery("query-name")
    .SetResultTransformer( Transformers.AliasToBean<Foo>())
    .SetString("name", "foo")
    .List<Foo>();

var list = session.CreateSQLQuery("SELECT * FROM table WHERE A=? and B=?")
    .SetString(0, "foo")
    .SetInt(1, 42)
    .SetResultTransformer( Transformers.AliasToBean<Foo>())
    .List<Foo>();

如果您为透视查询定义了视图,则可以使用 mutable="false" 为其定义只读映射类.

If you defined a view for a pivot query, you could define a readonly mapped class for it with mutable="false".

结果转换器

有许多结果转换器可用,包括:

A number of result transformers are available, including:

  • Transformers.ToList 返回一个 list 每行的值.
  • Transformers.AliasToEntityMap 创建一个 映射从列名到每一行的值.
  • Transformers.AliasToBean() 为每行创建一个指定类型的对象,并使用同名列中的值设置其属性.
  • Transformers.ToList Returns a list of values for each row.
  • Transformers.AliasToEntityMap Creates a map from column names to values for each row.
  • Transformers.AliasToBean() Creates an object of the nominated type per row and sets its properties with the value from the column of the same name.

这篇关于NHibernate、数据透视表和/或 GetNextID()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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