JPA 執(zhí)行update/delete query 需要加上事務(wù)

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

package com.easy.kotlin.picturecrawler.dao

import com.easy.kotlin.picturecrawler.entity.SearchKeyWord
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional


/**
 * Created by jack on 2017/7/17.
 */

interface SearchKeyWordRepository : PagingAndSortingRepository<SearchKeyWord, Long> {
    @Modifying
    @Transactional
    @Query(value = "INSERT INTO `search_key_word` (`deleted_date`, `gmt_created`, `gmt_modified`, `is_deleted`, `key_word`) VALUES (now(), now(), now(), '0', :keyWord) ON DUPLICATE KEY UPDATE `gmt_modified` = now()", nativeQuery = true)
    fun saveOnNoDuplicateKey(@Param("keyWord") keyWord: String): Int
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • spring data jpa @Query注解中delete語句報(bào)錯(cuò) 項(xiàng)目中需要?jiǎng)h除掉表中的一些數(shù)據(jù) @Quer...
    光劍書架上的書閱讀 3,326評(píng)論 3 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,652評(píng)論 18 399
  • 雨水模糊了記憶 記憶隔離了愛恨
    梵十七閱讀 301評(píng)論 15 15
  • 在長白山腳下,有這么一戶人家,夫妻倆生病相繼去世了,家里只剩下哥倆。哥哥和弟弟相依為命,一年四季,哥倆都以砍柴為生...
    千原閱讀 2,092評(píng)論 1 2

友情鏈接更多精彩內(nèi)容