Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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
关于Hibernate集合的IN子句_Hibernate_Jpa_Orm_Hql - Fatal编程技术网

关于Hibernate集合的IN子句

关于Hibernate集合的IN子句,hibernate,jpa,orm,hql,Hibernate,Jpa,Orm,Hql,我有两个实体:确认和行业。前者与后者有许多联系,反之亦然 @Entity public class Acknowledgement { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column private int id; @ManyToMany @JoinTable(name = "acknowledgement_industry", joinColumns = @Join

我有两个实体:
确认
行业
。前者与后者有许多联系,反之亦然

@Entity
public class Acknowledgement {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;

    @ManyToMany
    @JoinTable(name = "acknowledgement_industry", joinColumns = @JoinColumn(name = "acknowledgement_id"), inverseJoinColumns = @JoinColumn(name = "industry_id"))
    private Set<Industry> industries = new HashSet<>();
}

@Entity
public class Industry {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;

    @ManyToMany(mappedBy = "industries")
    private Set<Acknowledgement> acknowledgements = new HashSet<>();
}
这些参数是整数集。我也尝试了实体对象。对于
jpql
字符串,我尝试了以下查询(以及一些变体)

查询#1

select count(a) from Acknowledgement a where a.id in :acknowledgements and a.industries in :industries
结果

org.postgresql.util.PSQLException: No value specified for parameter 2.
org.hibernate.QueryException: illegal attempt to dereference collection [acknowledg0_.id.industries] with element property reference [id]
查询#2

select count(a) from Acknowledgement a where a.id in :acknowledgements and a.industries.id in :industries
结果

org.postgresql.util.PSQLException: No value specified for parameter 2.
org.hibernate.QueryException: illegal attempt to dereference collection [acknowledg0_.id.industries] with element property reference [id]
这种方法适用于其他一些关联类型,但显然不适用于集合

问题在于
行业协会的
IN
条款。我可以编写本机查询,但我想避免这样做。如何查找具有关联的
B
类型对象且其ID位于给定集合中的
A
类型的实体

我希望我说清楚了。谢谢。

请尝试下一个查询:

SELECT COUNT( DISTINCT a) FROM Acknowledgement a INNER JOIN a.industries AS ind where a.id in :acknowledgements and ind.id IN :industries

或者,您可以使用
的成员,而不是
表达式中的
:WHERE:industry1 a.industries的成员或:industry2 a.industries的成员或…
受Andrei的回答启发,我提出了以下似乎可行的查询。我以前试过,但我缺少的是
COUNT
函数中的
DISTINCT
关键字。与接受的答案相比,我还将连接更改为
内部连接
,并在
I.id
上使用
IN
子句,而不仅仅是别名
I

SELECT COUNT(DISTINCT a) FROM Acknowledgement a INNER JOIN a.industries i WHERE a.id IN :acknowledgements AND i.id IN :industries

你知道一种更具动态性的方法吗?这样我就不必根据我的集合中有多少个行业来操纵我的查询了?我将调查
成员-谢谢。我没有听说过更具活力的方式。动态方式的问题在于,不清楚是否应使用
运算符,即1<代码>其中:行业1 a.industries成员或:行业2 a.industries成员
WHERE:industry1 a.industries成员和:industry2 a.industries成员
我在您的查询中做了一些更改,似乎找到了一个可行的解决方案。请看我的答案。我接受这个答案是因为你为我指明了正确的方向。感谢您的帮助。与此问题相关的是,应该使用id属性的路径,而不是实体的路径:这实际上取决于您的参数是什么。根据我的经验:如果您有一个
Long
s列表作为参数,那么您必须使用id的路径,否则您可以同时使用这两个路径。