SpringData : xxx_xxx is not mapped

來源:互聯網
上載者:User

標籤:語句   遇到   技術   分享圖片   sele   factor   pac   實現   最好   

今天寫一個查詢語句的時候,報錯如下

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘tag_ArticleRepository‘:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query method public abstract java.util.List com.fdzang.mblog.repository.Tag_ArticleRepository.getByTag_oId(java.lang.String)! No property tag found for type Tag_Article!

查詢Repository如下

package com.fdzang.mblog.repository;import com.fdzang.mblog.pojo.Tag_Article;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;public interface Tag_ArticleRepository extends JpaRepository<Tag_Article,String> {    List<Tag_Article> getByTag_oId(String tag_oId);}

一開始想到的是肯定是底線會出問題,因為JPA對於命名規範比較嚴格,於是使用@Query註解進行測試

 @Query("SELECT ta FROM tag_article ta WHERE ta.tag_oId = :tag_oId")    List<Tag_Article> getByTag_oId(@Param("tag_oId") String tag_oId);

報錯如下

tag_article is not mapped

後來經過一番挫折,終於找到錯誤在哪兒了

 @Query("SELECT ta FROM Tag_Article ta WHERE ta.tag_oId = :tag_oId")    List<Tag_Article> getByTag_oId(@Param("tag_oId") String tag_oId);

在@Query註解中,應該是實體的名稱,而非資料庫的表名,區分大小寫

這個應該是Hibernate的底層實現原因,怪自己對Hibernate不熟悉而造成的

 

最後,總結一下:

以上是尚矽谷佟剛老師的SpringData教程,大致意思是在SpringDate的簡單查詢中,底線有著特定的意思

他的解析會優先於在類裡對比屬性

如本次的 getByTag_oId() 就會解析為Tag_Article.Tag.oId 而不是Tag_Article.Tag_oId

因為底線的優先順序比較高,因此會先解析底線,而後在類裡進行比較

 

遇到這種問題的時候我們就可以基於註解開發了,畢竟SpringData的簡單查詢雖然簡單,但是因為死板,所以命名方面比較苛刻

 

當然,最好的就還是別用這該死的底線了!哈哈

SpringData : xxx_xxx is not mapped

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.