英文:
Not able to delete in @OneToMany relationship spring data jpa
问题
在我的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 = GenerationType.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 = GenerationType.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);
}
英文:
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);
}
答案1
得分: 3
以下是翻译好的部分:
这种行为发生在已持久化于当前会话中的已删除对象。
for (QuickPopValue qpValue : quickPopValues) {
// 在这里你删除了 qpValue,但是这个对象仍然持久化在 `quickPopValues` 数组中,该数组是
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());
}
英文:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论