In my spring boot project, I have one LineItem entity below is the code
@Entity
@Table(name = "scenario_lineitem")
@Data
@NoArgsConstructor
public class LineItem implements Cloneable {
    private static Logger logger = LoggerFactory.getLogger(GoogleConfigConstant.class);
    @Id
    @GeneratedValue(strategy = IDENTITY)   
    private BigInteger lineItemId;
    @Column
    private String name; 
    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "line_item_meta_id")
    private List<QuickPopValue> quickPopValues;   
}
Another entity is
@Entity
@Table(name = "quick_pop_value")
@Data
@NoArgsConstructor
public class QuickPopValue implements Cloneable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "quick_pop_value_id", columnDefinition = "bigint(20)", unique = true, nullable = false)
    private BigInteger quickPopValueId;
    @Column(name = "column_name")
    private String columnName;
    @Column(name = "value")
    private String value;
    @Column(name = "formula", columnDefinition = "longtext")
    private String formula;
}
Now I am trying to delete QuickPopValue one by one but it's not getting deleted and not getting any exception as well.
Below is the delete code :
List<QuickPopValue> quickPopValues = sheetRepository.findByColumnName(columnName);
for (QuickPopValue qpValue : quickPopValues) {
    quickPopValueRepository.delete(qpValue);
}                       
Such behavior occurs when deleted object persisted in the current session.
for (QuickPopValue qpValue : quickPopValues) {
    // Here you delete qpValue but this object persisted in `quickPopValues` array which is 
    quickPopValueRepository.delete(qpValue);
}   
To solve this you can try delete by id
@Modifying
@Query("delete from QuickPopValue t where t.quickPopValueId = ?1")
void deleteQuickPopValue(Long entityId);
for (QuickPopValue qpValue : quickPopValues) {
    quickPopValueRepository.deleteQuickPopValue(qpValue.getQuickPopValueId());
}  
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With