NHibernate“;“数据库”;架构混淆[。\hibernate映射\@schema]

NHibernate“;“数据库”;架构混淆[。\hibernate映射\@schema],nhibernate,schema,nhibernate-mapping,mapping,Nhibernate,Schema,Nhibernate Mapping,Mapping,我主要针对一个MSSQL数据库使用NHibernate,其中我对各种表使用了MSSQL模式 在我的NH映射(HBM)文件中,我为映射中的每个表指定了模式,如下所示: <?xml version="1.0"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" schema="xyz">

我主要针对一个MSSQL数据库使用NHibernate,其中我对各种表使用了MSSQL模式

在我的NH映射(HBM)文件中,我为映射中的每个表指定了模式,如下所示:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   schema="xyz">                 <!-- schema specified -->
  <class name="Customer">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="Name" />
  </class>
</hibernate-mapping>
public string GetQualifiedName(NHibernate.Dialect.Dialect dialect)
{
    string quotedName = this.GetQuotedName(dialect);
    return ((this.schema == null) ? 
        quotedName : 
        (this.GetQuotedSchemaName(dialect) + '.' + quotedName));
}

对于我的单元测试,我一直在尝试SQLite,但是我的映射现在失败了,因为NH报告说找不到数据库“xyz”

我理解在解释方面存在差异,那么什么是NH的解释/实现,以及使用schema的最佳方法是什么


顺便说一句:使用诸如“nhibernate数据库模式”之类的关键字搜索web不会产生任何相关信息。

nhibernate.Mapping.Table类有一个
GetQualifiedName(nhibernate.dialen.dialen dial)
方法,该方法定义如下:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   schema="xyz">                 <!-- schema specified -->
  <class name="Customer">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="Name" />
  </class>
</hibernate-mapping>
public string GetQualifiedName(NHibernate.Dialect.Dialect dialect)
{
    string quotedName = this.GetQuotedName(dialect);
    return ((this.schema == null) ? 
        quotedName : 
        (this.GetQuotedSchemaName(dialect) + '.' + quotedName));
}

因此,除了为每个场景提供一组单独的映射(或在编译之前对它们进行预处理)之外,基本上没有办法让SQLite忽略模式名称。

您可以使用属性
default\u schema
在配置文件中指定模式(如果需要)。您可以使用多个配置文件,或者更改正在使用的配置文件—一个用于生产,另一个用于测试

您可以简单地忽略模式设置并使用不同的凭据。

标准的解释是表有一个由三部分组成的名称:“CATALOG.schema.table”:这些名称在标准(ISO-SQL标准?)的“information\u schema”视图中使用。Hibernate(大概也是NHibernate)遵循这个约定,您可以在类映射中指定catalog和schema,在配置中指定default_catalog和default_schema

在我自己的单元测试环境(使用Hypersonic)中,我在构建SessionFactory之前对Hibernate配置进行了调整:我自己这样做是为了设置与HSQL兼容的IdentifierGenerators,但您可能需要清除映射类的模式属性


一般来说,我尽量避免在应用程序中指定模式和目录。在Oracle中,我通常创建同义词,以便用户在自己的命名空间中看到对象;在PostgreSQL中,在数据库配置中设置搜索路径;在SQL Server中,将所有表放入“dbo”中。

我目前使用的是NH 2.0.1 GA。不过,看看2.1.0 Alpha3版,HBM XSD现在似乎包含一个catalog属性。我们将进一步调查。