gpt4 book ai didi

java - QueryDSL,Hibernate 在删除父表行时删除子表行

转载 作者:行者123 更新时间:2023-11-30 11:33:59 25 4
gpt4 key购买 nike

我正在尝试删除父表行并观察它是否在子表行上级联(删除)。具有 java 注释的父表实体和子表实体是:

//Table details
@Entity
@Table(name="PARENT_TABLE")
//Mandatory Column details
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="PARENT_TABLE_ID")
private Integer id;
.
.
.
@OneToMany(cascade={CascadeType.ALL}, mappedBy = "parentTable")
private Set<ChildTable> setChildTable;
//Child table entity details:
@Entity
@Table(name = "CHILD_TABLE")
//Column details
@Id
@Column(name = "PARENT_TABLE_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
.
.
private ParentTable parentTable;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "PARENT_TABLE_ID")
public ParentTable getPatentTable() {
return parentTable;
}

//QueryDSL to Delete child table row, looks like this:
HibernateDeleteClause query = new HibernateDeleteClause(getSession(),QChildTable.childTable);
Path<?> idPath = QChildTable.childTable;
query.where(((NumberPath<?>)idPath).in((Number[]) ids)).execute();
//QueryDSL to Delete parent table rows, looks like this:
HibernateDeleteClause query = new HibernateDeleteClause(getSession(),QParentTable.parentTable);
Path<?> idPath = QParentTable.parentTable;
query.where(((NumberPath<?>)idPath).in((Number[]) ids)).execute();

如果我删除子项,然后尝试删除父表行,它工作正常。寻求帮助以插入方式一次删除父表和子表行。与创建分配数据和插入的 ParentTable 对象一样,插入父表和子表行。感谢您的帮助。

最佳答案

不幸的是,JPQL 中的 DELETE 子句不会级联到相关实体,因此您需要使用 API 进行级联删除或更新:

A delete operation only applies to entities of the specified class and its subclasses. 
It does not cascade to related entities.

关于java - QueryDSL,Hibernate 在删除父表行时删除子表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892198/

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