Swastik
Swastik

Reputation: 396

deleteByField query method is failing though table has values

In my spring boot app I'm using query method deleteByCollectioId(someValue) (where CollectionId is the field name) to delete all entries present in my DB with given value.

Code

  @Transactional
  @Modifying
  void deleteByCollectionId(Long collectionId);

This code worked fine for many days but recently few times this started giving me an error saying

Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1; nested exception is org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1

I am not able to find root cause as this error is not reproducable most of the times. Is there anything wrong in Entity Class or Code?

Entity Class

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ToString(doNotUseGetters = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Builder
@Entity
@Table(name = "myCollection")
public class Collection {
  @Id
  @Column(name = "id")
  @GeneratedValue(generator = "auto_inc_id")
  @GenericGenerator(name = "auto_inc_id", strategy = "increment")
  private Long id;


  @Column(name = "collection_id")
  @JsonProperty(value = "collection_id")
  private Long collectionId;

  ..
  ..
  ..

}

Note: Here collectionId is not unique across multiple rows, there can be multiple rows with same collectionId

Upvotes: 0

Views: 108

Answers (1)

raminr
raminr

Reputation: 804

Is it possible that collection ID is not unique across all collections? Your primary key seems to be id. How do you guarantee collection ID is unique?

You may want to implement your own equals/hash method.

Also you don't need @Modifying annotation. It's only used when you use the Query annotation. Spring Data Doc

Upvotes: 1

Related Questions