删除未完成

时间:2018-10-05 14:30:29

标签: hibernate spring-boot jpa spring-data

我使用Spring Boot 2,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
    @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> {
}

如果我删除了采样,样本,测试样本和压缩,则应删除。

我的删除

@Transactional
public void deleteSamplings(int year, Integer id) {
    samplingsRepository.deleteById(new SamplingsPK(year, id));

}

调用此方法时,我看到了

  

删除       从           样本       哪里           sample_letter =?           和sampled_id =?           和sample_year =?

     

2018-10-03 22:21:05.832错误14511 --- [nio-8080-exec-9]   o.h.i.ExceptionMapperStandardImpl:HHH000346:发生错误   托管刷新[批量更新从更新返回了意外的行数   [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()   路径[]中的servlet [dispatcherServlet]抛出异常   [请求处理失败;嵌套异常为   org.springframework.orm.ObjectOptimisticLockingFailureException:批处理   update从更新[0]返回意外行数;实际行   计数:0;预期:1;嵌套异常为   org.hibernate.StaleStateException:批处理更新返回意外   更新[0]中的行数;实际行数:0;预期:1]与根   原因       在com.lcm.service.SamplingsService $$ EnhancerBySpringCGLIB $$ d589edcb.deleteSamplings()   〜[main /:na]

没有查询样品和其他

只需搜索删除所有内容的方法...

2 个答案:

答案 0 :(得分:0)

这可能是因为您试图更新/删除没有差异或已不存在的内容。

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

如果显式设置Id属性的值,将导致此错误。在Hibernate映射文件中,检查字段generator =“ native”或“ incremental”,在您的数据库中,映射的表未自动递增。

还尝试更新表格以设置auto_increment

答案 1 :(得分:0)

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

pip install virtualenv==15.* --upgrade

考虑到您要通过delete from samples where sample_letter=? and sampling_id=? and sampling_year=?进行删除,where子句不应包含条件id。这是因为sample_letter = ?没有有关new SamplingsPK(year, id)的信息。我将调查为sample_letter参数传递什么值。

另外,在sample_letter类中有两个字段标记为@IdSamplings上在语义上似乎不正确。

我建议您从字段@IdClass(SamplesPK.class)中删除@Id或使用键sampleLetter sampleLetteryear创建另一个IdClass。