Hibernate 删除不完整
我使用SpringBoot2、jpa和hibernate。 Db是postgres 我尝试删除具有子对象的对象Hibernate 删除不完整,hibernate,spring-boot,jpa,spring-data,Hibernate,Spring Boot,Jpa,Spring Data,我使用SpringBoot2、jpa和hibernate。 Db是postgres 我尝试删除具有子对象的对象 @Entity @IdClass(SamplingsPK.class) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Samplings { @Id private int year; //only last 2 number 2018 -> 18 @Id
@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> {
}
当调用此方法时,我明白了
删除
从…起
样品
哪里
样本字母=?
和采样_id=?
和采样年=
2018-10-03 22:21:05.832错误14511---[nio-8080-exec-9]
o、 h.i.异常外观标准MPL:HH000346:运行期间出错
托管刷新[批处理更新从更新返回意外的行数
[0];实际行数:0;预期:1]2018-10-03 22:21:05.834信息
14511---[nio-8080-exec-9]o.h.e.j.b.internal.AbstractBatchImpl:
HH000010:批处理发布时,它仍然包含JDBC语句
2018-10-03 22:21:05.848错误14511---[nio-8080-exec-9]
o、 a.c.c.c.[/].[dispatcherServlet]:的Servlet.service()
路径为[]的上下文中的servlet[dispatcherServlet]引发异常
[请求处理失败;嵌套异常为
org.springframework.orm.ObjectOptimisticLockingFailureException:批处理
更新从更新[0]返回了意外的行数;实际行数
计数:0;应为1;嵌套异常为
org.hibernate.StaleStateException:批处理更新返回了意外结果
更新[0]中的行数;实际行数:0;应为:1],根
原因
在com.lcm.service.samplingservice$$EnhancerBySpringCGLIB$$d589edcb.deleteSamplings()上
~[main/:na]
没有对示例和其他的查询
只需搜索一种删除所有内容的方法…这可能是因为您正在尝试更新/删除一些没有区别或不再存在的内容 此外,如果使用任何生成器类,则不应使用setter显式设置ID属性(这似乎不是您的情况) 如果显式设置Id属性的值,将导致此错误。请在Hibernate映射文件中检查字段生成器=“native”或“incremental”,并且在数据库中映射的表不会自动递增
另外,尝试更新表以设置自动增量生成的删除查询有点可疑
从样本中删除,其中样本字母=?和样本id=?和样本年份=?
考虑到您是通过id
删除的,where子句不应包括条件sample\u letter=?
。这是因为新采样pk(year,id)
没有关于sample\u letter
的信息。我将调查sample\u letter
参数传递的值
另外,在采样
类中有两个字段被注释为@Id
,这在@IdClass(SamplesPK.class)
中似乎语义不正确
我建议从字段
sampleLetter
中删除@Id
,或者用键sampleLetter
year
创建另一个IdClass,并且sequenceId
我使用postgres…所以它是sequenceOh好的,我没有注意到这一点。仅供参考:实际行数:0表示找不到记录更新;更新:0表示找不到任何记录,因此没有任何更新;预期:1表示预期db表中至少有1条带键的记录。同样,在查找记录、检查寄存器、缓存、打开事务时似乎出现问题。只是在此处验证该场景。似乎spring数据没有正确执行其任务。我发现一个实体中有许多Id注释。如果要使用复合键,为什么不使用EmbeddedId机制?为什么在sampleLetter类中的sampleLetter上有@Id注释?
@Transactional
public void deleteSamplings(int year, Integer id) {
samplingsRepository.deleteById(new SamplingsPK(year, id));
}