其中两个表格Transaction
和TxTargets
的实体如下所示:
Transaction.java:
@Entity
public class Transaction {
@Id
@GeneratedValue
private Long id;
private BigDecimal amount;
@OneToMany(cascade= CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "tx_id")
@IndexColumn(name="idx")
private List<TxTargets> txTargets;
}
TxTargets.java
@Entity
public class TransactionAllocation {
@Id
@GeneratedValue
private Long id;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private TargetType type;
private BigDecimal amount;
}
我需要从集合中移除0个值的TxTarget对象,其中多个元素具有相同的tx_id。由于OneToMany关系和@IndexColumn,我无法直接从TxTarget中删除它们。
为了理解数据的外观,这里有一个例子:
Transaction:
id|amount|txTargets
1 200 [1,2]
2 100 [3]
3 300 [4,5,6]
TxTargets
id|type|amount|idx|tx_id
1 tp1 200 0 1
2 tp1 0 1 1
3 tp2 100 0 2
4 tp2 150 0 3
5 tp2 0 1 3
6 tp2 150 2 3
TxTargets with ids 2 and 5 are expected to be deleted.
目前我按以下方式删除它们:
1)检索具有0值的所有TxTarget实体的ID,其中&#34;具有计数(tx_id)&gt; 1&#34;
2)从(1)中检索具有TxTargets的所有Transaction实体。
3)根据(1)
中的id列表从集合中删除TxTarget对象4)坚持交易实体。
这种方式可以正常工作,但我认为它缺乏性能(至少有几次收集迭代)并且通常具有冗余操作。
我想知道是否有更好的方法来消除这些诱惑,它们是什么?