简体   繁体   English

弹簧实体更新查询检查结果

[英]spring entity update query check result

I have entity class accountant. 我有实体类会计师。 I am trying save updated values of it. 我正在尝试保存它的更新值。 I am unable to do it. 我做不到。

I dont see any error logs . 我没有看到任何错误日志。 How do I check if the update query executed successfully or not 如何检查更新查询是否成功执行

@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);
}

In my accountantServiceImpl.java class I do 在我的accountantServiceImpl.java类中

public void updateAccountant(Accountant acct) {

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

hibernet save() will update only when primary key (most of the cases it is "id" field) is set in the object which you passing to save() method. 仅当在传递给save()方法的对象中设置了 主键 (大多数情况下是“ id”字段)时,hibernet save()才会更新。 If it does not find, it tries to insert a new record, which will succeed incase you don't have any DB level constraints. 如果找不到,它将尝试插入新记录,如果您没有任何数据库级别的约束,它将成功。

JPD also provides to have your own custom implementation. JPD还提供了自己的自定义实现。 You can do this. 你可以这样做。

  1. Create an interface AccountantRepositoryCustom and add the method you want eg updateAccountant . 创建一个接口AccountantRepositoryCustom并添加所需的方法,例如updateAccountant

    public interface AccountantRepositoryCustom { void updateAccountant(); }

  2. Extend your repository with this customer interface 使用此客户界面扩展存储库

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

  1. Create an implementation class AccountantRepositoryImpl for AccountantRepositoryCustom 创建AccountantRepositoryCustom一个实现类AccountantRepositoryImpl

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

NOTE : The name of custom interface and implementation class are importantant as JPA follows some convesation to detent the mapping. 注意:自定义接口和实现类的名称很重要,因为JPA遵循一些惯例来阻止映射。 So custom repository must ends with Custom and name of implementation class should end with Impl (you can override this settings, but its default which will do in most of the cases) 因此,自定义存储库必须以Custom结尾,实现类的名称应以Impl结尾(您可以覆盖此设置,但默认情况下,它将在大多数情况下使用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM