繁体   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