Hibernate如何映射SQL数据类型nvarchar(max)? [英] How can Hibernate map the SQL data-type nvarchar(max)?

查看:569
本文介绍了Hibernate如何映射SQL数据类型nvarchar(max)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL-2005数据库中有一列曾经是 varchar(max) ,但它已更改为 nvarchar(max)



现在我需要更新我的hibernate映射文件以反映更改,这就是过去:


< element type =textcolumn =Value/>



当我尝试运行应用程序时,出现以下错误:


org.hibernate .HibernateException:列值为[Table]中错误的列类型。找到:ntext,expected:text


我应该在'type'属性中将正确的列映射为 nvarchar(max)



我尝试将类型设置为 ntext 不知道那是什么。我尝试将类型设置为 string ,但将 string 当作 text 类型处理。

解决方案

Tremend Tech Blog 找到答案。您必须编写自己的SQLServerDialect类,它看起来像这样:



pre $ public class SQLServerNativeDialect extends SQLServerDialect {
public SQLServerNativeDialect(){
super();
registerColumnType(Types.VARCHAR,nvarchar($ l));
registerColumnType(Types.CLOB,nvarchar(max));

$ b $ public String getTypeName(int code,int length,int precision,int scale)throws HibernateException {
if(code!= 2005){
return super .getTypeName(code,length,precision,scale);
} else {
returnntext;




$ b $ p $这个类将Hibernate的类型映射为SQL类型,因此该类将 nvarchar(max) SQL数据类型映射到Hibernate的 CLOB 数据类型。


$ b

当Hibernate使用代码2005询问数据类型时, getTypeName 方法用于返回ntext它看起来像是nvarchar(max)数据类型)。

最后,您需要将您的hibernate持久性方言改为这个新的SQLServerDialect类,它允许hibernate转换数据键入SQL数据类型。


I have a column in my SQL-2005 database that used to be a varchar(max), but it has been changed to an nvarchar(max).

Now I need to update my hibernate mapping file to reflect the change, and this is what it used to be:

<element type="text" column="Value"/>

When I try to run the application, the following error appears:

org.hibernate.HibernateException: Wrong column type in [Table] for column Value. Found: ntext, expected: text

What should I put in the 'type' attribute to correctly map the column as an nvarchar(max)?

I've tried setting the type to ntext, but hibernate didn't know what that was. I tried setting the type to string, but it treated string as a text type.

解决方案

Found the answer at Tremend Tech Blog. You have to write your own SQLServerDialect class, it looks something like this:

public class SQLServerNativeDialect extends SQLServerDialect {
     public SQLServerNativeDialect() {
         super();
         registerColumnType(Types.VARCHAR, "nvarchar($l)");
         registerColumnType(Types.CLOB, "nvarchar(max)");
     }

    public String getTypeName(int code, int length, int precision, int scale) throws HibernateException {
        if(code != 2005) {
            return super.getTypeName(code, length, precision, scale);
        } else {
            return "ntext";
        }
    }
}

This class maps Hibernate's types to SQL types, so the class will map the nvarchar(max) SQL Data Type to Hibernate's CLOB data type.

The getTypeName method is used to return "ntext" when Hibernate asks about the data type with code 2005 (which looks like it's the nvarchar(max) data type).

Finally, you need to change your hibernate persistence dialect to this new SQLServerDialect class, which allows hibernate to translate data types into SQL data types.

这篇关于Hibernate如何映射SQL数据类型nvarchar(max)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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