【问题标题】:Update date timetsamp in parent when child is updated spring and mysql当孩子更新spring和mysql时更新父母中的日期时间戳
【发布时间】:2019-02-26 19:06:27
【问题描述】:

以下映射:

病历实体:

@ManyToOne(cascade={CascadeType.DETACH,
                        CascadeType.MERGE,
                        CascadeType.PERSIST,
                        CascadeType.REFRESH})
    @JoinColumn(name="patient_id")
    @JsonBackReference(value="patient_ref")
    private Patient patient;

处方实体:

@ManyToOne(cascade={CascadeType.DETACH,
                        CascadeType.MERGE,
                        CascadeType.PERSIST,
                        CascadeType.REFRESH})
    @JoinColumn(name="med_rec_id")
    @JsonBackReference
    private MedicalRecord medicalRecord;

控制器:

@PostMapping("/patients/medical-records/{id}/prescriptions")
    public Prescription addPrescription(@PathVariable int id,
                                        @RequestBody Prescription prescription){

        MedicalRecord medicalRecord = medicalRecordService.getRecord(id); 
        medicalRecord.addPrescription(prescription);

        return prescriptionService.addPrescription(prescription);
    }

我在 sql 中的医疗记录中有一个日期列,其中包含创建和更新时间戳。

我希望在更新处方(孩子)或插入新数据时更新医疗记录(父母)上的日期(时间戳)。

当我保存处方时,我相信它也应该保存作为医疗记录的父母。

但是,它似乎没有更新病历(父母)上的日期。

非常感谢您的帮助! 谢谢!

【问题讨论】:

  • 你能找到解决办法吗?

标签: spring spring-boot jpa spring-data-jpa


【解决方案1】:

您可以使用回调@PreUpdate、@PostUpdate Refer this

可以使用注解@LastModifiedDate Refer this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多