【问题标题】:SQL Server dialect with Hibernate带有 Hibernate 的 SQL Server 方言
【发布时间】:2016-06-12 16:30:55
【问题描述】:

我在我的项目中为 CRUD 层和 ORM 使用由 hibernate 支持的 spring 数据。我首先使用H2。但是在切换到 SQL Server 2014 时,我遇到了以下问题: 我使用以下服务:

@Query("Select example from Example example where
    example.exampleProperty like CONCAT('%',:param,'%')")
List<Example> findByProductLibe(@Param("param") String param);

使用属性获取示例对象(来自示例表)。它在 H2 中运行良好,但移动到 sql server(通过将配置通道和方言切换到 sql server)由于 Hibernate 生成的查询如下所示,我有一个 BadSqlGrammarException:

Hibernate: 
    select
        ex.param1 as param1,
        ex.param2 as param2            
    from
    example ex 
where
    example.exampleProperty like ('%'||?||'%') 

问题在于“|”字符,它会打印 'Incorrect syntax near '|' '

这是我的数据库配置:

    database.driver                    = com.microsoft.sqlserver.jdbc.SQLServerDriver
    database.password                  = 
    database.username                  = 
    hibernate.dialect                  = org.hibernate.dialect.SQLServerDialect
    hibernate.ejb.naming_strategy      = org.hibernate.cfg.ImprovedNamingStrategy
    hibernate.hbm2ddl.auto             = create
    hibernate.generate_statistics      = true
    hibernate.format_sql               = true
    hibernate.show_sql                 = true

感谢任何帮助或指示。

【问题讨论】:

  • 请添加您的 Hibernate 配置。

标签: sql-server hibernate spring-data


【解决方案1】:

在 repo 中替换为以下代码应该可以工作。

@Query("Select example from Example example where
    example.exampleProperty like %:param%")
List<Example> findByProductLibe(@Param("param") String param);

【讨论】:

    【解决方案2】:

    感谢您的回答,问题终于解决了,我将 hibernate.dialect 设置为 org.hibernate.dialect.SQLServer2012Dialect 并生成以下查询:

    Hibernate: 
    select
        ex.param1 as param1,
        ex.param2 as param2            
    from
    example ex 
    where example.exampleProperty like ('%'+?+'%')
    

    我一定没有正确清理和安装项目。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 1970-01-01
      • 2013-12-07
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      相关资源
      最近更新 更多