java MSSQL 2014 的错误休眠方言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33521144/
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-11-02 21:48:39  来源:igfitidea点击:

Wrong hibernate dialect for MSSQL 2014

javahibernatewildfly

提问by Marcin Roguski

I have a problem with inserting entities, that use sequences, to a MSSQL 2014 database. I use hibernate that is shipped with Wildfly 10 CR4 (but in CR1 and CR2 I got the same issue).

我在将使用序列的实体插入 MSSQL 2014 数据库时遇到问题。我使用 Wildfly 10 CR4 附带的休眠(但在 CR1 和 CR2 中我遇到了同样的问题)。

Here is a general info on the webapp run environment:

以下是有关 webapp 运行环境的一般信息:

  1. Wildfly 10 (CR4)
  2. Java 8 u 51
  3. Windows 7 Proffesional 64bit
  4. MSSQL Server 2014
  5. MSSQL driver: sqljdbc42.jar is deployed on the application server
  1. 野蝇 10 (CR4)
  2. 爪哇 8 u 51
  3. Windows 7 专业版 64 位
  4. MSSQL 服务器 2014
  5. MSSQL驱动:sqljdbc42.jar部署在应用服务器上

My persistence.xml file looks like this:

我的 persistence.xml 文件如下所示:

<persistence-unit name="mb_managed_pu" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:/jdbc/datasource</jta-data-source>
    <properties>
        <property name="hibernate.archive.autodetection" value="class, hbm" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.jdbc.batch_size" value="0" />
        <property name="hibernate.default_schema_" value="openmap"/>
        <property name="hibernate.connection.useUnicode" value="yes"/>
        <property name="hibernate.connection.characterEncoding" value="UTF-8"/>
    </properties>
</persistence-unit>

Now here is what happens when I run into an error.

现在这是当我遇到错误时会发生什么。

First, when Wildfly is started, I can see this warning:

首先,当 Wildfly 启动时,我可以看到这个警告:

WARN [org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver] (ServerService Thread Pool -- 68) HHH000385: Unknown Microsoft SQL Server major version [12] using SQL Server 2000 dialect

警告 [org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver] (ServerService Thread Pool -- 68) HHH000385:未知的 Microsoft SQL Server 主要版本 [12] 使用 SQL Server 2000 方言

I looked through the web and found that this problem is already known since January 2015, but unfortunately it is still an open issue.

我查看了网络,发现这个问题从 2015 年 1 月就已经知道了,但不幸的是它仍然是一个悬而未决的问题

The error itself is raised when I try to persist a new entity that has the ID configured to use sequences:

当我尝试保留一个新的实体,该实体的 ID 配置为使用序列时,会引发错误本身:

@Id
@Column(name = "MAP_BOOKMARK_ID")
@SequenceGenerator(name = "SEQ_MAP_BOOKMARKS", sequenceName = "SEQ_MAP_BOOKMARKS", allocationSize = 1)
@GeneratedValue(generator = "SEQ_MAP_BOOKMARKS", strategy = GenerationType.SEQUENCE)
private long                    id;

The exception raised is as follows:

引发的异常如下:

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name ?SEQ_MAP_BOOKMARKS”.

com.microsoft.sqlserver.jdbc.SQLServerException: 无效的对象名称?SEQ_MAP_BOOKMARKS”。

This is not a surprise since hibernate is using the wrong dialect - the one that does not know anything about sequences.

这并不奇怪,因为 hibernate 使用了错误的方言 - 对序列一无所知的方言。

When I modify persistence.xml and add this line:

当我修改persistence.xml并添加这一行时:

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect"/>

everything works like a charm.

一切都像一个魅力。

The problem is that the application will also work with Oracle database on another server and on Postgres on another. I'd like to avoid having to prepare multiple versions of the same application.

问题是该应用程序还可以与另一台服务器上的 Oracle 数据库和另一台服务器上的 Postgres 一起使用。我想避免必须准备同一个应用程序的多个版本。

Does anybody know of a solution to this problem ? Or should I wait for another Wildfly and/or hibernate version to appear ?

有人知道这个问题的解决方案吗?还是我应该等待另一个 Wildfly 和/或休眠版本出现?

采纳答案by Rodrigo Menezes

Meanwhile the team does not solve this problem, you can create a custom dialect resolver:

同时团队没有解决这个问题,你可以创建一个自定义的方言解析器:

public class ScopeStandardDialectResolver implements DialectResolver {


private static final long serialVersionUID = 1L;

    @Override
    public Dialect resolveDialect(DialectResolutionInfo info) {
        Dialect customDialectResolver = customDialectResolver(info);
        Log.getInstance().logInfo(Thread.currentThread().getStackTrace(), customDialectResolver.getClass().getName());
        return customDialectResolver;
    }

    private Dialect customDialectResolver(DialectResolutionInfo info) {
        final String databaseName = info.getDatabaseName();
        final int majorVersion = info.getDatabaseMajorVersion();
        if (isSqlServer2014(databaseName, majorVersion)) {
            return new SQLServer2012Dialect(); 
        } else {
            return StandardDialectResolver.INSTANCE.resolveDialect(info);
        }
    }

    private boolean isSqlServer2014(final String databaseName, final int majorVersion) {
        return databaseName.startsWith("Microsoft SQL Server") && majorVersion == 12;
    }

}

Then you configure in your persistence unit:

然后在持久化单元中进行配置:

<property name="hibernate.dialect_resolvers" value="com.oki.scope.hibernate.ScopeStandardDialectResolver" />

Based in this example: http://blog.exxeta.com/2016/03/23/dynamically-resolve-hibernate-database-dialect/

基于这个例子:http: //blog.exxeta.com/2016/03/23/dynamically-resolve-hibernate-database-dialect/

回答by garodriguezlp

I think that your problem could be related to this known issue HHH-9570. (The link actually contains my pull request to my proposed workaround)

我认为您的问题可能与这个已知问题HHH-9570 有关。(该链接实际上包含我提出的解决方法的拉取请求)

Basically, as it happened before with SQL Server 2012, the StandardDialectResolverof Hibernate does not recognise SQL Server 2014 ([Major version 12]) and then the default dialect SQLServerDialect is returned, which is the one for SQL Server 2000

基本上,就像 SQL Server 2012 之前发生的那样,Hibernate的StandardDialectResolver无法识别 SQL Server 2014([主要版本 12]),然后返回默认方言 SQLServerDialect,这是 SQL Server 2000 的方言

回答by Rashad Saif

For Springboot 1.4.7 and lower add below to properties file

对于 Springboot 1.4.7 及更低版本,在属性文件中添加以下内容

spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.SQLServer2012Dialect

回答by user6788533

Actually this problem comes due to non proper mapping of tables. Check tables and Entity class mappings.

实际上这个问题是由于表的不正确映射造成的。检查表和实体类映射。