使用Hibernate的SQL Server方言

时间:2016-02-29 15:06:43

标签: sql-server hibernate spring-data

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

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

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

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

问题在于&#39; |&#39;字符,它打印&#39; |&#39;附近的语法不正确&#39;

这是我的数据库配置:

    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

感谢您提供任何帮助或指示。

2 个答案:

答案 0 :(得分:0)

在repo中替换以下代码应该可行。

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

答案 1 :(得分:0)

感谢您的回答,问题终于得到解决,我将 hibernate.dialect 发送到 org.hibernate.dialect.SQLServer2012Dialect ,并生成以下查询:

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

我必须没有正确清理和安装项目。

谢谢。