文章出處
文章列表
spring使用jpa進行update操作主要有兩種方式:
1、調用保存實體的方法
1)保存一個實體:repository.save(T entity)
2)保存多個實體:repository.save(Iterable<T> entities)
3)保存并立即刷新一個實體:repository.saveAndFlush(T entity)
注:若是更改,entity中必須設置了主鍵字段,不然不能對應上數據庫中的記錄,變成新增(數據庫自動生成主鍵)或報錯(數據庫不自動生成主鍵)了
2、@Query注解,自己寫JPQL語句
使用JPA中@Query 注解實現update 操作,代碼如下:
@Transactional @Modifying(clearAutomatically = true) @Query(value = "update StockOut sc set sc.receivedPersonId=?1,sc.receivedPerson=?2,sc.receivedDate=?3 where stockOutCode=?4") int receipt(Long uid, String uname, Date createDate, String soCode);
備注:
1.更新StockOut表下一些字段, 這里使用了不是原生的sql語句,所以不要加nativeQuery = true。
2.@Transactional 注解用于提交事務,若沒有帶上這句,會報事務異常提示。
3.@Modifying(clearAutomatically = true) 自動清除實體里保存的數據。
文章列表
全站熱搜