SchemaExport创建/删除,nvarchar的长度始终为1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SchemaExport创建/删除,nvarchar的长度始终为1相关的知识,希望对你有一定的参考价值。

我想在测试中创建/删除模式:

[SetUp]
public void Setup()
{
   var config = new Configuration();

  config.Configure(Path.Combine(Environment.CurrentDirectory, "hibernate.cfg.xml"));
  (new[] { typeof(Entity).Assembly }).ToList().ForEach(a => config.AddAssembly(a));

  var export = new SchemaExport(config);

   export.Create(false, true);
}

[TearDown]
public void TearDown()
{
   var config = new Configuration();

  config.Configure(Path.Combine(Environment.CurrentDirectory, "hibernate.cfg.xml"));
  (new[] { typeof(Entity).Assembly }).ToList().ForEach(a => config.AddAssembly(a));

  var export = new SchemaExport(config);

   export.Drop(false, true);
}

生成表时,所有nvarchar列的长度为1.您能解释一下为什么?

这是映射的示例:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="TSS.Domain" namespace="TSS.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Status" table="DicStatus" schema="dbo" lazy="false">
    <id name="Id" type="Guid">
      <column name="id" not-null="true" sql-type="uniqueidentifier" />
      <generator class="guid.comb" />
    </id>
    <property name="Name" type="String">
      <column name="name" not-null="true" length="256" sql-type="nvarchar" />
    </property>
    <property name="CreatedAt" type="DateTime">
      <column name="createdAt" not-null="true" sql-type="datetime" />
    </property>

  </class>
</hibernate-mapping>

这是配置:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">***;</property>
    <property name="adonet.batch_size">100</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="max_fetch_depth">1</property>
    <property name="command_timeout">60</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
   </session-factory>
</hibernate-configuration>
答案

长度仅在您使用默认列类型时使用,因此当您没有指定qazxsw poi时。所以,如果你写

sql-type

它将使用<property name="Name" type="String"> <column name="name" not-null="true" length="256" /> </property> 生成name列,因为nvarchar(256)是MsSql2008Dialect中字符串的默认列类型。

或者您可以明确指定nvarchar,但在您的情况下,您不能使用长度,但您需要写出完整类型sql-type

nvarchar(256)

以上是关于SchemaExport创建/删除,nvarchar的长度始终为1的主要内容,如果未能解决你的问题,请参考以下文章

hibernate中使用schemaExport生成数据表报错解决方法

为啥 hbm2ddl.SchemaExport 不在此处运行?

schemaExport 构建失败,带有 Hibernate 的 HelloWorld 程序

Grails 抛出错误 hbm2ddl.SchemaExport - HHH000389:不成功

hibernate笔记--通过SchemaExport生成数据库表

在 JUnit/Spring 测试期间触发 Hibernate 的 SchemaExport.execute (hbm2ddl.auto) 的原因是啥?