是否有另一种选项可以在不使用 QueryDsl 的情况下通过包含实体的 pk 删除实体? (

Is there another option to delete the entity by pk of included entity without using QueryDsl? (

我正在尝试使用包含另一个实体的实体的 pk 删除一些实体。

这意味着, 我有一个名为 'A' 的实体。 它有另一个实体 B.

我想使用 B 的 PK 删除我的 A 实体。

但是我无法在 JpaRepository 中找到一些方法,除了 'deletByB'.

我不想使用 delete By Entitiy 删除我的实体 A。

public interface ARepository extends JpaRepository<AEtt, Long>{
    void deleteByB(BEtt b);
}

我找到了另一种方法。

使用 QueryDslRepositorySupport。

@Repository("BRepository")
public class BExtendRepositoryImpl extends QueryDslRepositorySupport implements BExtendRepository{
public BExtendRepositoryImpl() {
    super(BEtt.class);
}

@Override
public void deleteByAId(long aId) {
    QBEtt b = QBEtt.bEtt;
    JPQLQuery query = from(b);
    query.where(b.a.aId.eq(aId));
}

}


但是我想用JpaRepository。 我可以通过使用包含的实体的 PK 和 JpaRepository 来删除我的实体吗?

我希望在这里得到一些提示。谢谢!祝你今天过得愉快! :-)

假设您的实体 A 是 Order 并且 B 是 Customer 并且它们共享一对多关系,其中 Order.customer 指向相关的客户对象并且 Customer.id是客户的PK, 你应该能够实现如下:

@Repository
public OrderRepository extends JpaRepository<Order, Long> {
    Long deleteByCustomer_Id(String id);
}

但下划线是可选的。