0

我正在尝试将 Hibernate 中的 NativeQuery 功能与 SQL 查询中的 JOIN 和子查询一起使用,当我传递带有 IN 条件的参数列表时它不起作用。

代码:

Query query1 = entityManager.createNativeQuery("select product0_.* from TEST.PRODUCT product0_ \n" +
    "LEFT JOIN\n" +
    "(select DISTINCT product1_.PRODUCT_ID from TEST.PRODUCT_GROUP_PERMISSION product1_ \n" +
    "where product1_.GROUP_ID = 101 and product1_.PERMISSION_TYPE_ID in (:permissionTypes)\n" +
    ") \n" +
    "AS ProductID ON product0_.PRODUCT_ID = ProductID.PRODUCT_ID where product0_.NAME=:productName and product0_.ACTIVE='1' \n" +
    "and product0_.APPLICATION_ID = AppID.APPLICATION_ID order by upper(display_version)")
    .setParameter("productName",productName)
    .setParameter("permissionTypes",permissionTypes);

错误:

javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法执行查询] 根本原因 com.microsoft.sqlserver.jdbc.SQLServerException:将 nvarchar 值“ViewWebApp”转换为数据类型 int 时转换失败。

4

1 回答 1

0

Hibernate 创建查询方法:

 Query cQuery = entityManager.createQuery("select product0_ from Product product0_ where product0_.name =:productN and product0_.active='1' and product0_.productId in (select product1_.application.applicationId from ApplicationGroupPermission product1_ where product1_.groups in (:myGroupIds) and product1_.permissionType.permissionTypeField in (:pList)) order by product0_.displayVersion asc ")
                .setParameter("productN",productName)
                .setParameter("pList",permissionTypes)
                .setParameter("myGroupIds",myGroups);
于 2020-12-02T02:15:27.827 回答