我将Spring Boot 2与jpa和hibernate实现一起使用
我的某些实体
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {
@Id
@GenericGenerator(name = "samplings_id_seq", strategy = "com.permacon.lcm.model.SamplingSequenceGenerator")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "samplings_id_seq")
private Integer id;
@OneToMany(mappedBy = "sampling", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Samples> samples = new ArrayList<>();
...
}
@Entity
public class Samples {
@EmbeddedId
private SampleId id;
@MapsId("samplingId")
@ManyToOne(optional = false)
private Samplings sampling;
@OneToOne(mappedBy = "sample", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private TestSamples testSamples;
...
}
@Entity
public class TestSamples {
@Id
@SequenceGenerator(name = "test_samples_id_seq", sequenceName = "test_samples_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_samples_id_seq")
private Integer id;
@OneToOne(fetch = FetchType.LAZY)
private Samples sample;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Absorptions absorptionTest;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Compressions compressionTest;
}
ALTER TABLE ONLY test_samples
ADD CONSTRAINT test_samples_id_constraint FOREIGN KEY (sample_sample_letter, sample_sampling_id) REFERENCES samples(sample_letter, sampling_id);
实际上,要删除示例,我使用迭代器,然后删除所需的一个。
for (Iterator iterator = samples.iterator(); iterator.hasNext();) {
Samples next = (Samples) iterator.next();
...
iterator.remove();
}
如果我删除不带TestSamples的样本,则工作正常,如果存在,则失败,并出现违规约束(test_samples_id_constraint)
然后我看到休眠状态对采样表进行了更新,在采样表中进行了删除,但是它与TestSamples无关,与absorptionTest和compressionTest无关。
有什么主意吗?
我需要手动执行某些操作还是删除操作应该是自动的?
我全都掌握了。
答案 0 :(得分:-1)
让我们添加有关TestSample实体中外键中的列的信息
@OneToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="sample_sample_letter", referencedColumnName="sample_letter"),
@JoinColumn(name="sample_sampling_id", referencedColumnName="sampling_id")
})
private Samples sample;