簡體   English   中英

彈簧實體更新查詢檢查結果

[英]spring entity update query check result

我有實體類會計師。 我正在嘗試保存它的更新值。 我做不到。

我沒有看到任何錯誤日志。 如何檢查更新查詢是否成功執行

@Repository("accountantRepository")
@Transactional
public interface AccountantRepository extends JpaRepository<Accountant, Long> {

     public final static String UPDATE_ACCOUNTANT = "update Accountant acct SET acct.password =:password, acct.phoneNumber =:phoneNumber,"
            + " acct.state =:state, acct.postCode =:postCode, acct.country =:country where acct.id =:accountantId ";

@Query(UPDATE_ACCOUNTANT)
 @Modifying
 void updateAccountant(@Param("password") String password, @Param("phoneNumber") String phoneNumber,
         @Param("state") String state, @Param("postCode") String postCode, @Param("country") String country,
         @Param("accountantId") String accountantId);
}

在我的accountantServiceImpl.java類中

public void updateAccountant(Accountant acct) {

    accountantRepository.updateAccountant(acct.getPassword(), acct.getPhoneNumber(), acct.getState(),
            acct.getPostCode(), acct.getCountry(), acct.getId());       
}

僅當在傳遞給save()方法的對象中設置了 主鍵 (大多數情況下是“ id”字段)時,hibernet save()才會更新。 如果找不到,它將嘗試插入新記錄,如果您沒有任何數據庫級別的約束,它將成功。

JPD還提供了自己的自定義實現。 你可以這樣做。

  1. 創建一個接口AccountantRepositoryCustom並添加所需的方法,例如updateAccountant

    public interface AccountantRepositoryCustom { void updateAccountant(); }

  2. 使用此客戶界面擴展存儲庫

public interface AccountantRepository extends JpaRepository<Accountant, Long> , AccountantRepositoryCustom

  1. 創建AccountantRepositoryCustom一個實現類AccountantRepositoryImpl

public class AccountantRepositoryImpl implements AccountantRepositoryCustom { @Override public void updateAccountant () { // you code here }

注意:自定義接口和實現類的名稱很重要,因為JPA遵循一些慣例來阻止映射。 因此,自定義存儲庫必須以Custom結尾,實現類的名稱應以Impl結尾(您可以覆蓋此設置,但默認情況下,它將在大多數情況下使用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM