【问题标题】:Delete not complete删除未完成
【发布时间】: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


【解决方案1】:

这可能是因为您正在尝试更新/删除一些没有区别或不再存在的内容。

此外,如果您使用任何生成器类,则不应使用 setter 显式设置 ID 属性(这似乎不是您的情况)。

如果显式设置 Id 属性的值,则会导致此错误。在 Hibernate 映射文件中检查字段 generator="native" 或 "incremental",并且在您的 DATABASE 中映射的表不是 auto_incremented。

同时尝试更新您的表格以设置 auto_increment

【讨论】:

  • 我使用 postgres... 所以它是序列
  • 哦,好吧,我没注意到。仅供参考:实际行数:0 表示找不到要更新的记录; update: 0 表示没有找到任何记录,因此没有任何更新;预期:1 表示预期至少 1 条记录与 db 表中的键。同样,在查找记录、检查您的寄存器、缓存、打开交易方面似乎存在问题。只是在这里验证场景。
  • 好像,spring 数据没有正确地完成它的工作。
【解决方案2】:

生成的删除查询有点可疑。

delete from samples where sample_letter=? and sampling_id=? and sampling_year=?

考虑到您是通过id 删除的,where 子句不应包含条件sample_letter = ?。这是因为new SamplingsPK(year, id) 没有关于sample_letter 的信息。我会调查为sample_letter 参数传递了什么值。

此外,在 Samplings 类中注释为 @Id 的两个字段在 @IdClass(SamplesPK.class) 的语义上似乎不正确。

我建议要么从字段 sampleLetter 中删除 @Id,要么使用键 sampleLetter yearsequenceId 创建另一个 IdClass。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2021-01-24
    • 2020-11-24
    • 2011-06-02
    • 2013-01-07
    • 1970-01-01
    • 2010-09-08
    • 2021-06-08
    相关资源
    最近更新 更多