[Java] 解決Spring JPA 於Query中自動加入”_” 的問題

使用Spring JPA 時, 當執行Query 時總會彈出 Invalid column name 及throw exception. 那是因為Spring 設定所致.Spring 預設使用 org.hibernate.cfg.ImprovedNamingStrategy 來進名database column 及attribute 的naming conversion. 即是它會將camel case (e.g. getElementById) 轉為snack case (e.g. GET_ELEMENT_BY_ID), 從而令建立出來的query 有機會不能順利執行.

其中一個解法是修改其naming strategy, 將其改為org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl, 語法如下:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.