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());
}
Assuming you are using Spring Data - CrudRepository provides deleteAll(Iterable<? extends T> entities)
better use it ie
quickPopValueRepository.deleteAll(quickPopValues)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.