Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以从与HQL的多个关联中批量删除?_Java_Hibernate_Hql - Fatal编程技术网

Java 是否可以从与HQL的多个关联中批量删除?

Java 是否可以从与HQL的多个关联中批量删除?,java,hibernate,hql,Java,Hibernate,Hql,如果是,语法是什么 假设我希望Foo的一个实例与Bar的所有实例不关联: 在SQL中,它只是: delete from FOO_BAR_MAPPING where FOO_ID = ? 在HQL中,我假设它类似于: delete from Bar.foos foos where foos.id = :id (其中,foos是Foo的映射集合) 但似乎是错误的,给出: org.hibernate.hql.ast.QuerySyntaxException: Bar.foos is not map

如果是,语法是什么

假设我希望Foo的一个实例与Bar的所有实例不关联: 在SQL中,它只是:

delete from FOO_BAR_MAPPING
where FOO_ID = ?
在HQL中,我假设它类似于:

delete from Bar.foos foos
where foos.id = :id
(其中,foos是Foo的映射集合)

但似乎是错误的,给出:

org.hibernate.hql.ast.QuerySyntaxException: Bar.foos is not mapped

HQL甚至可以做到这一点吗?

HQL不可能单独删除关联。然而,您可以做的是:

A) 假设您正在为单个Foo执行此操作,并且您的级联设置正确,您可以加载该
Foo
实例,清除其
条的集合并保存它


B) 如果您试图从多个
foo
中清除
,您可以映射一个SQL查询而不是HQL来回答您的特定问题,不,据我所知,HQL是不可能的

我认为您在这里稍微混合了SQL和HQL。在SQL中,确实可以“删除”记录,当然hibernate最终也会这样做。然而,Hibernate/HQL是基于面向对象的思想设计的,所以在这个上下文中“删除”意味着您正在删除对象,而不是关联。通常,您会执行以下操作:

Foo f = session.get(Foo.class, id);
f.getBars().clear();
session.merge(f);

这将按指定的id检索对象,并通过清除其集合来删除所有条关联。显然,必须将Foo-bar关联映射到正确的方向,才能使其正常工作。

注意,这只会删除关联表条目,而不会删除末尾的元素。为此,需要启用“删除孤立项”。