【发布时间】:2019-03-11 02:17:52
【问题描述】:
我使用 spring boot 2、jpa 和 hibernate。 数据库是postgres 我尝试删除一个有孩子的对象
@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> {
}
如果我删除 Samplings,Samples、TestSamples 和 Compressions 应该被删除。
我的删除
@Transactional
public void deleteSamplings(int year, Integer id) {
samplingsRepository.deleteById(new SamplingsPK(year, id));
}
当这个方法被调用时,我看到了
删除 从 样品 在哪里 样本字母=? 和sample_id =? 和 sampling_year=?
2018-10-03 22:21:05.832 错误 14511 --- [nio-8080-exec-9] o.h.i.ExceptionMapperStandardImpl:HHH000346:期间出错 managed flush [批量更新从更新返回了意外的行数 [0];实际行数:0;预计:1] 2018-10-03 22:21:05.834 信息 14511 --- [nio-8080-exec-9] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010:在批处理发布时,它仍然包含 JDBC 语句 2018-10-03 22:21:05.848 错误 14511 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] :Servlet.service() for 带有路径 [] 的上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.springframework.orm.ObjectOptimisticLockingFailureException:批处理 更新从更新 [0] 返回了意外的行数;实际行 计数:0;预期:1;嵌套异常是 org.hibernate.StaleStateException:批量更新返回意外 来自更新 [0] 的行数;实际行数:0;预期:1] 有根 原因 在 com.lcm.service.SamplingsService$$EnhancerBySpringCGLIB$$d589edcb.deleteSamplings() ~[main/:na]
没有样品和其他的查询
只需搜索一种删除所有内容的方法...
【问题讨论】:
-
我发现一个实体中有许多 Id 注释这一事实很奇怪。如果你想使用复合键为什么不使用 EmbeddedId 机制?
-
为什么 Samples 类的 sampleLetter 上有@Id 注解?
标签: hibernate spring-boot jpa spring-data