春天实体:返回创建的记录的ID [英] spring entity : return id of created record

查看:70
本文介绍了春天实体:返回创建的记录的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 AccountantRepository

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

AccountantServiceImpl

@Service("accountantService")
public class AccountantServiceImpl implements AccountantService{

    @Autowired
    private AccountantRepository accountantRepository;

    @Override
    public Accountant saveAccountant(Accountant newAccountant, String role) {
        return accountantRepository.save(newAccountant);
    }
}

当我这样做 accountantRepository.save(newAccountant);

如何获取新创建的记录的ID?

how do I obtain the id of the newly created record?

推荐答案

通过 JpaRepository.save()使用返回的实例.它将包含值的 id .

Use the returned instance by JpaRepository.save(). It will contain the id valued.

CrudRepository.save() 方法(其中声明了 save()的方法)指定:

使用返回的实例进行进一步的操作作为保存操作可能已经完全更改了实体实例.

Use the returned instance for further operations as the save operation might have changed the entity instance completely.

这篇关于春天实体:返回创建的记录的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆