I use spring boot 2, jpa and hibernate. Db is postgres I try to delete an object with child
@Entity
@IdClass(SamplingsPK.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {
@Id
private int year; //only last 2 number 2018 -> 18
@Id
@GeneratedValue
private Integer sequenceId;
@OneToOne
private Colors color;
@OneToMany(mappedBy = "sampling", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Samples> samples = new ArrayList<>();
@Embedded
private TestSamplings testSamplings;
...
}
public class SamplingsPK implements Serializable {
private int year;
private Integer sequenceId;
public SamplingsPK(int year, Integer sequenceId) {
this.sequenceId = sequenceId;
this.year = year;
}
private SamplingsPK() {
}
...
}
@Entity
@IdClass(SamplesPK.class)
public class Samples{
@Id
private String sampleLetter;
@Id
@ManyToOne(optional = false)
@JoinColumns({
@JoinColumn(name = "sampling_id", referencedColumnName = "sequenceId"),
@JoinColumn(name = "sampling_year", referencedColumnName = "year")})
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 Compressions compressionTest;
....
}
@Repository
public interface SamplingsRepository extends JpaRepository<Samplings, SamplingsPK> {
}
If i delete Samplings, Samples, TestSamples, and Compressions should be deleted.
My delete
@Transactional
public void deleteSamplings(int year, Integer id) {
samplingsRepository.deleteById(new SamplingsPK(year, id));
}
When this method is called I see
delete from samples where sample_letter=? and sampling_id=? and sampling_year=?
2018-10-03 22:21:05.832 ERROR 14511 --- [nio-8080-exec-9] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] 2018-10-03 22:21:05.834 INFO 14511 --- [nio-8080-exec-9] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements 2018-10-03 22:21:05.848 ERROR 14511 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1] with root cause at com.lcm.service.SamplingsService$$EnhancerBySpringCGLIB$$d589edcb.deleteSamplings() ~[main/:na]
there are no query for sample and others
just search a way to delete every thing...