2016-02-29 49 views
0

我正在爲我的CRUD圖層和ORM項目使用由hibernate支持的彈簧數據。我首先使用H2。但是切換到SQL Server 2014的時候,我遇到了以下問題:使用屬性帶休眠功能的SQL Server方言

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

得到實施例的對象(例如從表): 我用下面的服務。它在H2運作良好,但移動到SQL服務器(通過切換配置信道和方言到SQL Server)我有一個BadSqlGrammarException由於由Hibernate生成的查詢如下:

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

的問題是與'|'字符,它會打印'語法不正確'|' 「

這裏是我的數據庫配置:

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 

感謝任何幫助或指示。

+0

請添加您的Hibernate配置。 –

回答

0

替換爲以下代碼回購應該工作。

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

感謝您對您的回答,終於解決了,我做了我 hibernate.dialect org.hibernate.dialect.SQLServer2012Dialect並生成以下查詢:

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

我一定還沒有清理並正確安裝項目。

謝謝。