gpt4 book ai didi

java - 无法在@OneToMany 关系spring 数据jpa 中删除

转载 作者:行者123 更新时间:2023-12-01 14:17:15 25 4
gpt4 key购买 nike

在我的spring boot项目中,我有一个LineItem下面的实体就是代码

@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;
}

另一个实体是

@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;
}

现在我尝试一个一个地删除 QuickPopValue,但它没有被删除,也没有收到任何异常。

下面是删除代码:

List<QuickPopValue> quickPopValues = sheetRepository.findByColumnName(columnName);
for (QuickPopValue qpValue : quickPopValues) {
quickPopValueRepository.delete(qpValue);
}

最佳答案

当删除的对象保留在当前 session 中时,会发生这种行为。

for (QuickPopValue qpValue : quickPopValues) {
// Here you delete qpValue but this object persisted in `quickPopValues` array which is
quickPopValueRepository.delete(qpValue);
}

要解决这个你可以尝试通过id删除

@Modifying
@Query("delete from QuickPopValue t where t.quickPopValueId = ?1")
void deleteQuickPopValue(Long entityId);
for (QuickPopValue qpValue : quickPopValues) {
quickPopValueRepository.deleteQuickPopValue(qpValue.getQuickPopValueId());
}

关于java - 无法在@OneToMany 关系spring 数据jpa 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com