Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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 使用in运算符在多对多关系中查询JPA条件_Java_Sql_Hibernate_Jpa_Hibernate Criteria - Fatal编程技术网

Java 使用in运算符在多对多关系中查询JPA条件

Java 使用in运算符在多对多关系中查询JPA条件,java,sql,hibernate,jpa,hibernate-criteria,Java,Sql,Hibernate,Jpa,Hibernate Criteria,我有以下两个实体: 个人资料,类别 在配置文件实体中,我有一个配置文件拥有的类别列表 在类别实体中,我有一个类别拥有的配置文件列表 生成的表称为profiles\u具有\u类别,并具有以下列: 配置文件id、类别id 我想找到属于以下ID为1、2、3、4的任何类别的所有配置文件 我将在纯sql中使用以下命令执行此操作: SELECT p.* FROM profiles p, profiles_has_categories phc WHERE p.id = phc.profile_id AND

我有以下两个实体:

个人资料,类别

在配置文件实体中,我有一个配置文件拥有的类别列表

在类别实体中,我有一个类别拥有的配置文件列表

生成的表称为profiles\u具有\u类别,并具有以下列:

配置文件id、类别id

我想找到属于以下ID为1、2、3、4的任何类别的所有配置文件

我将在纯sql中使用以下命令执行此操作:

SELECT p.* FROM profiles p, profiles_has_categories phc 
WHERE p.id = phc.profile_id 
AND phc.category_id IN (1, 2, 3, 4)
但我不知道如何使用CriteriaBuilder在JPA中构造查询:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Profile> criteria = cb.createQuery(Profile.class);
Root<Profile> root = criteria.from(Profile.class);

// Profile_.categories is a collection of Category objects
// categories is collection of Long (category ids) that i want to search in
// but is not working
criteria.where(root.get(Profile_.categories).in(categories))

List<Profile> results = em.createQuery(criteria).getResultList();
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery-criteria=cb.createQuery(Profile.class);
Root=标准.from(Profile.class);
//概要文件。类别是类别对象的集合
//categories是我要搜索的长(类别ID)的集合
//但它不起作用
标准.where(root.get(Profile.categories).in(categories))
List results=em.createQuery(条件).getResultList();
你不想要get,你想要加入:


谢谢这就是我要找的。
criteria.where(root.join(Profile_.categories).in(categories))