4

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...

robert trudel
  • 5,283
  • 17
  • 72
  • 124
  • 1
    I found weird the fact that you have many Id annotations inside one entity. If you want to use composite key why don't use EmbeddedId mechanism ? – Woody Oct 16 '18 at 18:19
  • Why do you have @Id annotation on sampleLetter in Samples class? – chandra Oct 22 '18 at 14:31

2 Answers2

0

This is probably because you are trying to update/delete something that has no difference or doesn't exist anymore.

Besides that, the ID property should not be set explicitly using setters (this seems not to be your case), if you use any generator class.

If you set the value of the Id property explicitly, it will cause this error. Check in the Hibernate mapping file the field generator="native" or "incremental" and in your DATABASE the table mapped is not auto_incremented.

Also try to update your table to set auto_increment

jhenrique
  • 858
  • 1
  • 10
  • 17
  • i use postgres... so it's sequence – robert trudel Oct 16 '18 at 13:19
  • Oh okay, I didn't notice that. Just for your information: actual row count: 0 means no records where found to update; update: 0 means no records where found so there's nothing update; expected: 1 means expected at least 1 record with key in db table. Again, it looks like a problem in finding the records, check your registers, cache, open transactions. Just verifying the scenario here. – jhenrique Oct 16 '18 at 13:30
  • seem like, spring data is not doing it's job correctly. – robert trudel Oct 16 '18 at 14:00
0

Generated delete query is kind of suspicious.

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

Considering you are deleting by id, the where clause should not include condition sample_letter = ?. This is because new SamplingsPK(year, id) has no information about sample_letter. I would investigate what value is being passed for sample_letter parameter.

Also, having two fields annotated as @Id in Samplings class doesn't seem semantically correct with @IdClass(SamplesPK.class).

I would advise to either remove @Id from the field sampleLetter or create another IdClass with keys sampleLetter year, and sequenceId.

Kedar Joshi
  • 1,441
  • 11
  • 17