首页 文章

NHibernate“数据库”模式混淆[ . \ hibernate-mapping \ @schema]

提问于
浏览
3

我正在使用NHibernate主要针对MSSQL数据库,我在那里使用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>

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

我知道schema的解释存在差异,那么什么是NH 's interpretation/implementation, and what is the best approach when using schema'?

顺便说一句:使用像“nhibernate数据库模式”这样的关键字搜索网络并没有产生任何相关的东西 .

3 回答

  • 1

    “标准”解释是表有一个由三部分组成的名称:“CATALOG.SCHEMA.TABLE”:这些是标准(ISO-SQL标准?)“information_schema”视图中使用的名称 . Hibernate(可能也是NHibernate)遵循此约定,您可以在类映射中指定目录和模式,并在配置中指定default_catalog和default_schema .

    在我自己的单元测试环境中(使用Hypersonic),我在构建SessionFactory之前对Hibernate配置进行了按摩:我自己做了这样的事情,比如设置HSQL兼容的IdentifierGenerators,但是你可以通过清除映射类的模式属性 .

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

  • 1

    NHibernate.Mapping.Table 类有一个 GetQualifiedName(NHibernate.Dialect.Dialect dialect) 方法,定义如下:

    public string GetQualifiedName(NHibernate.Dialect.Dialect dialect)
    {
        string quotedName = this.GetQuotedName(dialect);
        return ((this.schema == null) ? 
            quotedName : 
            (this.GetQuotedSchemaName(dialect) + '.' + quotedName));
    }
    

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

  • 6

    您可以使用属性 default_schema 在配置文件中指定架构(如果需要) . 您可以使用多个配置文件,或者更改您正在使用的配置文件 - 一个用于 生产环境 ,另一个用于测试 .

    您可以简单地忽略架构设置并使用不同的凭据 .

相关问题