CrudRepository不删除行

时间:2019-05-15 13:01:13

标签: java spring spring-data-jpa spring-data

我已经创建了一个计划类

计划:

@Entity(name = "Plan")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PlanEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int planId;

    private String planName;

    private BigDecimal pBenefitMax;

    private LocalDate pStartDate;

    private LocalDate pEndDate;

    private int pCompanyId;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "dPlanId")
    private List<DentalAgreementEntity> dentalAgreementEntities;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "pPlanId")
    private List<PatientEntity> patientEntities;
}

患者实体看起来像这样:

@Entity(name = "Patient")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PatientEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int patientId;

    private String firstName;

    private String lastName;

    private String patientEmail;

    private String patientPhone;

    private LocalDateTime dateOfBirth;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "tPatientId")
    private List<TransactionEntity> transactionEntities;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "aPatientId")
    private List<AppointmentEntity> appointmentEntities;

    @ManyToOne
    @JoinColumn(name = "pPlanId")
    private PlanEntity planEntity;

}

协议实体看起来像这样:

@Entity(name = "DentalAgreement")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class DentalAgreementEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer agreementId;

    private BigDecimal percentage;

    private Integer dPlanId;

    private String dAdaCode;
}

PlanRepository看起来像这样:

@Repository
public interface PlanRepository extends CrudRepository<PlanEntity, Integer> {
}

我能够轻松地成功保存计划实体。但是,如果我尝试删除PlanEntity,就像这样:

public void deleteCompanyPlan(PlanEntity planEntity){
    planRepository.delete(planEntity);
}

尽管没有引发错误,但是当我检查数据库时,该行并未删除。我试图使类可序列化,仅使用“ deleteById()”方法,以及其他许多方法,但是我无法通过Spring实现删除Plan。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

通过从父实体中删除FetchType.LAZY和CascadeType.All来解决此问题。

@Entity(name = "Company")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper=false)
public class CompanyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int companyId;

    private String companyName;

    private String companyNumber;

    private String companyEmail;

    private String companyAddress;

    private String companyCity;

    private String companyState;

    private String companyZip;

    @OneToMany
    @JoinColumn(name = "pCompanyId")
    private List<PatientEntity> patientEntities;

    @OneToMany
    @JoinColumn(name = "pCompanyId")
    private List<PlanEntity> planEntities;

    @Override
    public String toString() {
        return "CompanyEntity{" +
                "companyId=" + companyId +
                ", companyName='" + companyName + '\'' +
                ", companyNumber='" + companyNumber + '\'' +
                ", companyEmail='" + companyEmail + '\'' +
                ", companyAddress='" + companyAddress + '\'' +
                ", companyCity='" + companyCity + '\'' +
                ", companyState='" + companyState + '\'' +
                ", companyZip='" + companyZip + '\'' +
                '}';
    }
}

答案 1 :(得分:-1)

Spring Data CrudRepository支持delete操作而不是deleteById操作 参见https://www.concretepage.com/spring-5/spring-data-crudrepository-example#Transactional

好,可以使用Java文档here

我建议您更新您的deleteCompanyPlan()

@Transactional
public void deleteCompanyPlan(PlanEntity planEntity){
    planRepository.delete(3);
}

我也同意@ M.Deinum的观点,您很可能需要使用@Transactional注释来进行救援