不能在Spring Data JPA的@OneToMany关系中进行删除。

huangapple go评论85阅读模式
英文:

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 = &quot;scenario_lineitem&quot;)
@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 = &quot;line_item_meta_id&quot;)
    private List&lt;QuickPopValue&gt; quickPopValues;   
}

Another entity is

@Entity
@Table(name = &quot;quick_pop_value&quot;)
@Data
@NoArgsConstructor
public class QuickPopValue implements Cloneable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = &quot;quick_pop_value_id&quot;, columnDefinition = &quot;bigint(20)&quot;, unique = true, nullable = false)
    private BigInteger quickPopValueId;

    @Column(name = &quot;column_name&quot;)
    private String columnName;

    @Column(name = &quot;value&quot;)
    private String value;

    @Column(name = &quot;formula&quot;, columnDefinition = &quot;longtext&quot;)
    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&lt;QuickPopValue&gt; 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(&quot;delete from QuickPopValue t where t.quickPopValueId = ?1&quot;)
void deleteQuickPopValue(Long entityId);
for (QuickPopValue qpValue : quickPopValues) {
    quickPopValueRepository.deleteQuickPopValue(qpValue.getQuickPopValueId());
}  

huangapple
  • 本文由 发表于 2020年4月4日 21:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定