SQL 在休眠中映射到 varchar 和 nvarchar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1306774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:17:14  来源:igfitidea点击:

Mapping to varchar and nvarchar in hibernate

sqlhibernatevarcharnvarchar

提问by Niikola

If there are 2 columns in database, eg.

如果数据库中有 2 列,例如。

code varchar(3)
name nvarchar(50)

How to tell hibernate to pass varchar for searching by code?

如何告诉 hibernate 传递 varchar 以按代码搜索?

In the hibernate mappings string is mapped to nvarchar and it produces queries like:

在休眠映射字符串被映射到 nvarchar 并产生如下查询:

Select code, name From table where code=N'AAA'  (instead of code='AAA')

This is very bad as it causes index scan instead of index seek operation (scanning all index nodes instead of directly going to requested one)

这是非常糟糕的,因为它导致索引扫描而不是索引查找操作(扫描所有索引节点而不是直接转到请求的节点)

As code is used in millions of rows as well as in several indexes and foreign keys, changing it from varchar to nvarchar will cause performance degradation (more IO operations as nvarchar uses twice more space than varchar).

由于代码用于数百万行以及多个索引和外键,将其从 varchar 更改为 nvarchar 将导致性能下降(更多 IO 操作,因为 nvarchar 使用的空间是 varchar 的两倍)。

Is there any way to tell hibernate to do mapping according to database type, not to Java type?

有没有办法告诉hibernate根据数据库类型而不是Java类型进行映射?

Thanks

谢谢

回答by Gilberto Olimpio

Probably you already solved this, but I had a similar problem.

可能你已经解决了这个问题,但我遇到了类似的问题。

I'm using jTDS JDBC driver and I solved the index scan problem by adding:

我正在使用 jTDS JDBC 驱动程序,并通过添加以下内容解决了索引扫描问题:

;sendStringParametersAsUnicode=false;prepareSQL=0

to the end of the jTDS connection string.

到 jTDS 连接字符串的末尾。

Probably it would not had solved your problem because by doing this, jTDS will only use VARCHAR (no NVARCHAR anymore).

可能它不会解决您的问题,因为通过这样做,jTDS 将只使用 VARCHAR(不再使用 NVARCHAR)。

Also, I had to disable the prepared SQL, because Hibernate is using 'like' instead of '=' when generating the queries and by using 'like' combined with a variable (SELECT ... WHERE column LIKE @var) causes an index scan (MSSQL 2000).

此外,我不得不禁用准备好的 SQL,因为 Hibernate 在生成查询时使用 'like' 而不是 '=' 并且通过使用 'like' 结合变量(SELECT ... WHERE column LIKE @var)导致索引扫描(MSSQL 2000)。

回答by ChssPly76

I'm assuming you're talking about NHibernate rather than Hibernate because the latter does not use nvarchar in its default SqlServer dialect.

我假设您在谈论 NHibernate 而不是 Hibernate,因为后者在其默认的 SqlServer 方言中不使用 nvarchar。

The way to solve your problem is to specify column type as "AnsiString" in your mapping:

解决问题的方法是在映射中将列类型指定为“AnsiString”:

<property name="Code" type="AnsiString"/>

Take a look at this postfor more details.

看看这篇文章了解更多细节。

回答by Tanuj Verma

<type-mapping>
        <sql-type jdbc-type="NVARCHAR" hibernate-type="string" />
 </type-mapping>

Add the above code in the hibernate reveng file.

在 hibernate reveng 文件中添加以上代码。

回答by razvanone

In hibernate.properties set the property hibernate.connection.defaultNChar=false.

在 hibernate.properties 中设置属性 hibernate.connection.defaultNChar=false。

You can either hide your tables behind views or use nstring type. This type is available in hibernate-core 4.x. In hibernate-core 3.6.10.Final you will need to define the custom type nstring - see the comment in the url: Getting Hibernate and SQL Server to play nice with VARCHAR and NVARCHAR.

您可以将表格隐藏在视图后面或使用 nstring 类型。此类型在 hibernate-core 4.x 中可用。在 hibernate-core 3.6.10.Final 中,您需要定义自定义类型 nstring - 请参阅 url 中的注释: Getting Hibernate and SQL Server to play nice with VARCHAR and NVARCHAR