Spring Data JPA .save() 方法未保存在数据库中 [英] Spring Data JPA .save() method is not saving in the database

查看:94
本文介绍了Spring Data JPA .save() 方法未保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型:

public class ABC implements Serializable {
    private int baseId;
    private Integer aId;
    private Integer bId;
    private Boolean isReal;
    private TimeStamp updateTime;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "base_id", nullable = false)
    public int getBaseId() {
        return baseId;
    }
    public void setBaseId(int baseId) {
        this.baseId = baseId;
    }

    @Basic
    @Column(name = "a_id", nullable = false)
    public Integer getAId() {
        return aId;
    }

    public void setAId(Integer aId) {
        this.aId = aId;
    }

    @Basic
    @Column(name = "b_id", nullable = false)
    public Integer getBId() {
        return bId;
    }

    public void setBId(Integer bId) {
        this.bId = bId;
    }
    @Basic
    @Column(name = "is_real")
    public Boolean getIsReal() {
        return isReal;
    }

    public void setIsReal(Boolean isReal) {
        this.isReal = isReal;
    }

    @Basic
    @Column(name ="update_time")
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }
}

我有一个控制器类:

@RestController
@RequestMapping(path = "${serverconfig.api-base-path}/base")
public class BaseController {
    /**
     * Instance of an logger
     */
    private static final Logger LOG = 
        LoggerFactory.getLogger(BaseController.class);

    /**
     * Base repository
     */
    private BaseRepository baseRepository;

    /***
     *
     * @param baseRepository
     */
    public BaseController(BaseRepository baseRepository) {
        LOG.trace("BaseRepository constructor method.");
        this.baseRepository = baseRepository;
    }

    @PostMapping(path = Route.UPDATE_IS_REAL)
     // @Transactional
    public ABC updateIsReal(@Valid @RequestBody 
    @RequestParam("baseId") int baseId,

    @RequestParam("isReal") boolean isReal){
        ABC abc = baseRepository.findByBaseId(baseId);
        Date date= new Date();
        Timestamp ts = new Timestamp(date.getTime());
        abc.setBaseId(baseId);
        abc.setIsReal(isReal);
        abc.setUpdateTime(ts);

        return baseRepository.save(abc);

    }

}

我的存储库类:

 @Repository
 public interface BaseRepository extends 
 JpaRepository<ABC, Integer> {

    List<ABC> findByAId(Integer aId);

    ABC findByBaseId(Integer baseId);
}

数据库表有一个条目:

"base_id": 1,
"a_Id": 1,
"b_Id": 1,
"is_real": null,
"update_time": null

当我调用端点时,它没有给出错误并返回:

When I call the endpoint it gives no error and returns:

"base_id": 1,
"aId": 1,
"bId": 1,
"isReal": yes,
"updateTime": 018-10-01T18:30:56.765+0000

但是当我查询数据库时,那里的记录没有更新.我不明白我做错了什么.我在尝试进行休息调用时提供了 id,并且该 id 存在于数据库中.

But When I query the database, the record is not updated there. I am not understanding what I am doing wrong. I am supplying id when I try to make a rest call and that id exists in the database.

推荐答案

使用 save,更改不需要立即刷新到数据库,而可能只保留在内存中,直到刷新或提交命令发出.

With save, changes won't necessary be flushed to DB immediately and might stay just in memory, until flush or commit commands are issued.

使用saveAndFlush,更改将立即刷新到数据库.

With saveAndFlush, changes will be flushed to DB immediately.

但是,如果您刷新事务中的更改并且不提交它们,则在此事务中提交之前,这些更改仍然不会对外部事务可见.

However, if you flush the changes in transaction and do not commit them, the changes still won't be visible to the outside transactions until the commit in this transaction.

在您的 BaseController 中尝试更改

In your BaseController try changing

return baseRepository.save(abc);

return baseRepository.saveAndFlush(abc);

更多信息 这里这里

这篇关于Spring Data JPA .save() 方法未保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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