Java 如何在具有多列的子句中构建CriteriaQuery谓词?

Java 如何在具有多列的子句中构建CriteriaQuery谓词?,java,mysql,sql,hibernate,criteria,Java,Mysql,Sql,Hibernate,Criteria,给定a用于a,例如: Predicate predicate = root.get(MyTable.col1).in("col1Val1", "col1Val2"); 是否可以扩展到使用多个AND字段,例如与下面的SQL相同 SELECT * FROM MyTable WHERE (col1, col2, col3) IN ( ("col1Val1", "col2Val1", "col3Val1"), ("col1Val2", "col2Val2", "col3Val2") )

给定a用于a,例如:

Predicate predicate = root.get(MyTable.col1).in("col1Val1", "col1Val2");
是否可以扩展到使用多个AND字段,例如与下面的SQL相同

SELECT *
FROM MyTable
WHERE (col1, col2, col3) IN (
    ("col1Val1", "col2Val1", "col3Val1"),
    ("col1Val2", "col2Val2", "col3Val2")
);

不那么优雅的方法,使用JPA标准生成器

    Path<String> col1Path=root.get("col1");
    Path<String> col2Path=root.get("col2");
    Path<String> col3Path=root.get("col3");

    Predicate p0=criteriaBuilder.concat(col1Path,col2Path,col3Path)
         .in("col1Val1"||"col2Val1"||"col3Val1",
              "col1Val2"|| "col2Val2"|| "col3Val2");
路径col1Path=root.get(“col1”); Path col2Path=root.get(“col2”); Path col3Path=root.get(“col3”); 谓词p0=criteriaBuilder.concat(col1Path,col2Path,col3Path) .in(“col1Val1”| |“col2Val1”| |“col3Val1”, “col1Val2”| |“col2Val2”| |“col3Val2”); 第二种方法

    Path<String> col1Path=root.get("col1");
    Path<String> col2Path=root.get("col2");
    Path<String> col3Path=root.get("col3");

    Predicate p1=criteriaBuilder.or(
          criteriaBuilder.and(criteriaBuilder.equal(col1Path,"col1Val1"),
                              criteriaBuilder.equal(col2Path,"col2Val1"),
                              criteriaBuilder.equal(col3Path,"col3Val1") 
                              ), 
          criteriaBuilder.and(criteriaBuilder.equal(col1Path,"col1Val2"),
                   criteriaBuilder.equal(col2Path,"col2Val2"),
                   criteriaBuilder.equal(col3Path,"col3Val2") 
                   )
           );
路径col1Path=root.get(“col1”); Path col2Path=root.get(“col2”); Path col3Path=root.get(“col3”); 谓词p1=criteriaBuilder.or( criteriaBuilder.and(criteriaBuilder.equal)(col1Path,“col1Val1”), criteriaBuilder.equal(col2Path,“col2Val1”), criteriaBuilder.equal(col3Path,“col3Val1”) ), criteriaBuilder.and(criteriaBuilder.equal)(col1Path,“col1Val2”), criteriaBuilder.equal(col2Path,“col2Val2”), criteriaBuilder.equal(col3Path,“col3Val2”) ) );
谢谢你的回答!第一种方法可能存在连接字符串不能唯一标识单个字符串的问题(例如,列值“col1Val1col2”、“Val1col3”和“Val1”也会匹配)。第二种方法似乎更好-但在我的情况下,将有一个非常大的匹配值列表,因此我想知道性能将如何比较…刚刚找到了上述问题的答案(但遗憾的是,不是我想听到的):通常in子句无效,特别是对于大型比较集。JPA似乎不支持元组中的where子句,我试图准确地找到它,但没有成功,但无论如何,or-和解决方案可以工作,并且具有与in几乎相同的性能。如果你有比较大的集合,也许你可以找到一些更一般的条件